How do I loop through a PHP array containing data returned from MySQL?

后端 未结 6 1836
野趣味
野趣味 2020-12-21 18:10

Ok I have a table with a few fields. One of the fields is username. There are many times where the username is the same, for example:

6条回答
  •  独厮守ぢ
    2020-12-21 18:22

    You can also obtain a count of all rows in a table like this:

    
        $count = mysql_fetch_array(mysql_query("SELECT COUNT(*) AS count FROM table"));
        $count = $count["count"];
    

    You can also append a normal WHERE clause to the select above and only count rows which match a certain condition (if needed). Then you can use your count for loops:

        $data = mysql_query("SELECT * WHERE username='bob'");
        for ($i = 0; $i 

    Also, mysql_fetch_array() is usually a lot easier to use as it stores data in an associative array, so you can access data with the name of the row, rather than it's numeric index.

    Edit: There's some kind of bug or something going on where my second code block isn't showing everything once it's posted. It shows fine on the preview.

提交回复
热议问题