Hi all,

I'm retrieving some records from a MySQL database using a query (a while loop) in another query. Everything works fine except that the while loop prints out the same row twice, instead of two separate rows (I've only got 2 rows in that particular table at the moment for testing purposes).

Could there be some conflict between the first

$row = mysqli_fetch_array($r, MYSQLI_ASSOC); {

and the nested

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) ?

For my second query, I've changed some variables in my actual code so that they don't conflict ($row, $num etc).


The following code is just some sample code I created for demonstration purposes, so my query code isn't accurate but the focus is on how the second query (with while loop) is nested.

Could someone please have a look at this?

Thank you in advance.




PHP Code:

<?php

$q 
"SELECT name, address, books_read,  FROM table1 INNER JOIN table2 WHERE some_id = 5";        

$r = @mysqli_query ($dbc$q);     
$num mysqli_num_rows($r);
if (
$num 0) {
$row mysqli_fetch_array($rMYSQLI_ASSOC); {
 
Print 
'<div>
<p class="name">'
.$row['name'] . ' </p>
<p class="address">'
.$row['address'].'</p>
</div>'
;


$q "SELECT books_read FROM table1 INNER JOIN table2 WHERE some_id = 5";        

$r = @mysqli_query ($dbc$q);     
$num mysqli_num_rows($r);
if (
$num 0) {
while (
$row mysqli_fetch_array($rMYSQLI_ASSOC)) {
 
Print 
'<div><p class="books">'.$row['books_read'] . ' </p></div>';
}
}

Print 
'<div>
<p>other content</p>
</div>'
;

 }
}
?>