Home > Article > Backend Development > Why Does MySQL Only Return One Row When Using `mysql_fetch_assoc()` in PHP?
Why MySQL Returns Only One Row in PHP
In MySQL, using PHP's built-in mysql_* functions, a common issue occurs when the expected query result returns multiple rows but only the first row is accessible.
To resolve this, consider the following PHP code:
<code class="php">$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5"); $query2 = mysql_fetch_assoc($query); print_r($query2);</code>
This code fetches only the first row of the query result and displays it. To access the remaining rows, a while() loop should be employed:
<code class="php">$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5"); while ($row = mysql_fetch_assoc($query)) { print_r($row); }</code>
Additional Notes:
The above is the detailed content of Why Does MySQL Only Return One Row When Using `mysql_fetch_assoc()` in PHP?. For more information, please follow other related articles on the PHP Chinese website!