Looping Through SQL Results in PHP - Not getting Entire Array

后端 未结 2 840
一向
一向 2020-12-10 19:07

I\'m probably missing something easy, but I seem to be blocked here... I have a MySQL database with two tables and each table has several rows. So the goal is to query the

相关标签:
2条回答
  • 2020-12-10 19:44

    You need to use the following because if you call mysql_fetch_array outside of the loop, you're only returning an array of all the elements in the first row. By setting row to a new row returned by mysql_fetch_array each time the loop goes through, you will iterate through each row instead of whats actually inside the row.

    while($row = mysql_fetch_array($result))
    {
       // This will loop through each row, now use your loop here
    
    }
    

    But the good way is to iterate through each row, as you have only three columns

    while($row = mysql_fetch_assoc($result))
    {
       echo $row['name']." ";
       echo $row['email']." ";
    }
    
    0 讨论(0)
  • 2020-12-10 19:51

    One common way to loop through results is something like this:

    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        print_r($row);
        // do stuff with $row
    }
    

    Check out the examples and comments on PHP.net. You can find everything you need to know there.

    0 讨论(0)
提交回复
热议问题