MySQL仅返回一行 [英] MySQL returns only one row

查看:293
本文介绍了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仅返回一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆