本文介绍了MySQL 只返回一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的 PHP 代码:

I have this simple PHP code:

$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5");
    $query2 = mysql_fetch_assoc($quer);
    print_r($query2);

它只返回这个:

Array ( [title] => Kill Bill Vol 1. [url_title] => kill_bill_vol_1 )

我在表中有 3500 多行,在 PhpMyAdmin 中运行 SQL 效果很好.

I have 3500+ rows in the table, and running the SQL in PhpMyAdmin works perfectly.

推荐答案

$query = mysql_query("SELECT `title`,
                             `url_title`
                        FROM `fastsearch`
                       WHERE `tags`
                            LIKE '%$q%'
                       LIMIT 5");

while ($row = mysql_fetch_assoc($query)) {
    print_r($row);
}

  • 您在示例中拼错了 $query
  • mysql_fetch_assoc() 每次被调用时都会返回一行,当超出行时FALSE.通过在条件中为它分配一个变量来利用它.在 while() 循环中,$row 将是当前行.
    • You misspelled $query in your example
    • mysql_fetch_assoc() will return a row each time it is called, and FALSE when out of rows. Use that to your advantage, by assigning a variable to it in the condition. Within the while() loop, $row will be the current row.
    • 这篇关于MySQL 只返回一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 00:55