Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Multiple while($row = mysql_fetch_array($variable)) { } ERRORS

Okay my syntax can probably be described without the code. Basically it should be easy...but never is.

I have 2 loops in a row...basically the same thing. I SELECT * every variable from my database, and then I need to build a 2 layer javascript based on two sep. variables.

So

I have:

while ($row = mysql_fetch_array($myVariable)) {

// do events

}

then after that

while ($row2 = mysql_fetch_array($myVariable)) {

// do events

}

For some reason it's completely returning NOTHING on the second one...is there some where I need to reset my array, did it possibly end and then I can't just restart. Such a basic question, but I can't figure out what else could be wrong for the life of me. So thats the question, is that a general issue to just do that twice in a row and expect PHP to start over each time at the beginning of $myVariable which holds my SELECT statement?

Thanks...will check all day and update as needed. First timer to Stack Overflow i'd love to see the help.

like image 893
Alex Seidler Avatar asked Feb 07 '12 15:02

Alex Seidler


People also ask

How to fetch single row from database in PHP?

The fetch_row() / mysqli_fetch_row() function fetches one row from a result-set and returns it as an enumerated array.

How mysql_ fetch_ row works?

mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

What is mysql_ fetch_ array in PHP?

mysql_fetch_array is a PHP function that will allow you to access data stored in the result returned from the TRUE mysql_query if u want to know what is returned when you used the mysql_query function to query a Mysql database. It is not something that you can directly manipulate. Syntax.

Which function will get a result row as a numerically indexed array after the successful execution of a SELECT sql query?

The fetch_all() / mysqli_fetch_all() function fetches all result rows and returns the result-set as an associative array, a numeric array, or both. Note: This function is available only with MySQL Native Driver.


2 Answers

The problem is that the query returns a resource. This resource has it's own internal pointer (counter to remember which row to return). mysql_fetch_array will advance that internal pointer and return each row from the resource one at a time. once it is at the end (no more rows left), it returns false. so on the last loop, $row equals false which exits the while loop. The pointer is not reset though. So on the next while loop, you call mysql_fetch_array, the pointer is at the end and so it returns false which completely bypasses the second while loop. What you should do, is loop through the data once and store each row into an array. Then you can copy/loop over the array as much as you want and everything works as expected.

$data = array();
while ($row = mysql_fetch_array($myVariable)) {
    $data[] = $row;
}
//now $data has all the rows. a simple foreach will loop over the data.
foreach($data as $row){
    //do events
    print_r($row);
}

//and you can loop over data again and again.
foreach($data as $row){
    //do events
    print_r($row);
}
like image 162
Jonathan Kuhn Avatar answered Oct 19 '22 04:10

Jonathan Kuhn


Before the second loop, try using mysql_data_seek():

mysql_data_seek($myVariable, 0);

The pointer in the recordset needs to be reset, otherwise it will still indicate that it's at the end.

It may be a better option, as ZiTAL points out, to store your records in an array and just iterate over the array.

like image 24
Wiseguy Avatar answered Oct 19 '22 04:10

Wiseguy