Asked  1 Year ago    Answers:  5   Viewed   6 times

I made my scripts without class. (framework). Now I have changed my mind & I wanna make my scripts with classes. I like it. But there I have a problem.. Infinite Loop While. I didn't take this problem before with without classes..

While Rule;

while ($fetch=$db->fetchObject($db->q("SELECT * FROM auction_house.items ORDER BY id DESC LIMIT ".$min_stuff.",".$max_stuff."")))
{
    // etc
}

Class;

public function fetchObject($queryRes)
{
    $this->queryRes = $queryRes;
    $this->record = mysql_fetch_object($this->queryRes);
    return $this->record;
}

When I try to while loop, I am taking infinite loop on page. How can I solve this problem?

 Answers

4

You executing query in each iteration. It always returns a new resource.

Try to separate query and cycle:

$res = $db->q("SELECT * FROM auction_house.items ORDER BY id DESC LIMIT ".$min_stuff.",".$max_stuff."");
while ($fetch=$db->fetchObject($res))
{
    // etc
}
Thursday, April 1, 2021
 
Xatoo
 
3

The way mysql_fetch_assoc() works is that it has an internal pointer, and every time you run the method, you get the next item and the pointer is moved one time. If there was no pointer, how would the while loop otherwise ever terminate? It would just keep pulling out colours, over and over.

The way to resolve this is to reset the point in each iteration. This can be done using mysql_data_seek().

$product_query = mysql_query('SELECT * FROM products WHERE id > 0');
$colour_query = mysql_query('SELECT * FROM product_colours WHERE id > 0');

while ($get_product_rows = mysql_fetch_assoc($product_query)) {
    echo $get_product_rows['name'];
    echo "<br />";

    if ($get_product_rows['has_colour'] == '1') {
        while ($get_colour_row = mysql_fetch_assoc($colour_query)) {
            // Drop down box population goes here. 
        }
    }
    mysql_data_seek($colour_query, 0);
}
Thursday, April 1, 2021
1

I would certainly suggest using a JOIN here that way you can get all this data in one go. I will add one caveat however - this would take more memory that querying the database in a loop like you are currently doing, so if you have a large number of rows in either table, this might not be a suitable approach.

The query would simply be:

SELECT * FROM opdrachen
INNER JOIN resultaten ON opdrachen.id = resultaten.opdrachenid

You can then set up an array that is keyed on each id very simply like this:

$array = array();
while ($row = mysql_fetch_array($result)) {
    $array[$row['id']][] = $row;
}

This give you an easy way to iterate through the results, one id at a time like this:

foreach ($array as $id => $rows_for_id) {
      // output stuff for row group
      foreach ($rows_for_id as $resultaten_row) {
          // output stuff for resultaten rows
      }
}

A few other suggestions:

  • Don't use mysql_* functions as they are deprecated. Learn how to use mysqli or PDO.
  • Don't use SELECT * queries. Its is lazy and wastes bandwidth, transfer time, and system memory. Only query for the specific fields you are actually interested in. So replace the * in my example with the actual field you need.
Thursday, April 1, 2021
 
qitch
 
5

kill the process. assuming you can get access to the console via ssh and your server runs on linux:

ps -ef | grep php // to get a list of php-processes

kill [process-id] // kill the process by process-id
Thursday, August 12, 2021
 
jcubic
 
3

Check that 'loaded' is definitely declared as volatile.

Explanation: if a variable is read and/or written by multiple threads, then you need to take appropriate thread-safety measures. One such thread-safety measure is volatile, which is suitable for primitive values (or object references) which are read or written as 'simple' actions with the value written on a given occasion not depending on the previously read value. For more information, I have an article about volatile on my web site (along with other information about thread-safety generally) that may be of help.

Tuesday, August 17, 2021
 
macha
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :