php MySQL 只返回一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4372197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL returns only one row
提问by wintercounter
I have this simple PHP code:
我有这个简单的 PHP 代码:
$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5");
$query2 = mysql_fetch_assoc($quer);
print_r($query2);
It only returns this:
它只返回这个:
Array ( [title] => Kill Bill Vol 1. [url_title] => kill_bill_vol_1 )
I have 3500+ rows in the table, and running the SQL in PhpMyAdmin works perfectly.
我在表中有 3500 多行,在 PhpMyAdmin 中运行 SQL 效果很好。
回答by alex
$query = mysql_query("SELECT `title`,
`url_title`
FROM `fastsearch`
WHERE `tags`
LIKE '%$q%'
LIMIT 5");
while ($row = mysql_fetch_assoc($query)) {
print_r($row);
}
- You misspelled
$query
in your example mysql_fetch_assoc()
will return a row each time it is called, andFALSE
when out of rows. Use that to your advantage, by assigning a variable to it in the condition. Within thewhile()
loop,$row
will be the current row.
- 你
$query
在你的例子中拼错了 mysql_fetch_assoc()
每次调用FALSE
时都会返回一行,并且在行外返回。通过在条件中为它分配一个变量来利用它。在while()
循环内,$row
将是当前行。
回答by hamczu
As documentation http://php.net/manual/en/function.mysql-fetch-assoc.phpstates:
作为文档http://php.net/manual/en/function.mysql-fetch-assoc.php指出:
mysql_fetch_assoc — Fetch a result row as an associative array
So if you want to iterate over result you have to use a loop e.g.:
因此,如果您想迭代结果,则必须使用循环,例如:
while ($row = mysql_fetch_assoc($result)) {
echo $row["title"];
echo $row["url_title"];
}
回答by Orbling
Right, you are not fetching the results properly.
是的,您没有正确获取结果。
mysql_fetch_assoc()
only returns one row at a time. Use a loop to read all rows.
mysql_fetch_assoc()
一次只返回一行。使用循环读取所有行。
$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5");
$resultSet = array();
while ($cRecord = mysql_fetch_assoc($query)) {
$resultSet[] = $cRecord;
}
回答by Freddie
the method fetch_assoc() returns one row, you need to loop with it
方法 fetch_assoc() 返回一行,你需要用它循环
回答by Pooja Khatri
mysqli_fetch_assoc()
is functions which is fetching multiple records and displaying through print_r().
mysqli_fetch_assoc()
是获取多条记录并通过print_r() 显示的函数。
$query = "SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5";
$result = mysqli_query($conn,$query);
while(null ! == ($query = mysqli_fetch_assoc($result))){
print_r($query);
}