MySQL Only Returning One Row from a Query
Problem:
When executing a SQL query that should return multiple rows, only one row is being retrieved using PHP's mysql_query() and mysql_fetch_assoc(). The SQL query executed directly in PhpMyAdmin returns all expected rows.
Solution:
Option 1: Using a Loop to Iterate Over Rows
$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5"); while ($row = mysql_fetch_assoc($query)) { print_r($row); }
Option 2: Using mysql_fetch_array() to Retrieve an Array of Rows
$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5"); $rows = array(); while ($row = mysql_fetch_array($query)) { $rows[] = $row; }
Note:
The above is the detailed content of Why is my MySQL query only returning one row when executed in PHP?. For more information, please follow other related articles on the PHP Chinese website!