Home > Backend Development > PHP Tutorial > Why Is My MySQL Query Returning Only One Row When I Expect Multiple?

Why Is My MySQL Query Returning Only One Row When I Expect Multiple?

DDD
Release: 2024-11-02 18:31:30
Original
739 people have browsed it

Why Is My MySQL Query Returning Only One Row When I Expect Multiple?

MySQL Returns Multiple Rows Rather Than One

When retrieving data from a database using MySQL, it's possible to encounter situations where only one row is returned, despite expecting multiple. To address this issue, consider the following explanation.

In the example provided, the SQL query in PhpMyAdmin returns multiple rows successfully. However, the PHP code does not handle the retrieval of these rows correctly. Here's a modified version of the PHP code that will output all rows matching the query:

<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>
Copy after login

In this corrected code:

  • The $query variable is correctly spelled.
  • The mysql_fetch_assoc() function is used within a while loop to iterate through and print each row returned by the query.
  • Each row is stored in the $row variable and output using print_r().

The above is the detailed content of Why Is My MySQL Query Returning Only One Row When I Expect Multiple?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template