当我在我的文件 search.php 中使用以下内容查询我的数据库时,它只返回它遇到的第一个结果。
$qry = "SELECT business_id FROM business WHERE zip like '%91326%'";
$rs = mysql_query($qry);
$rec = mysql_fetch_array($rs);
echo $session->showContents($rec);
showContents 只是一个实用函数...
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
showContents 返回了这个:
Array
(
[0] => 3
[business_id] => 3
)
疯狂的是,当我在 sqlbuddy 中输入相同的查询时,它会给我:
business_id
3
5
6
我很迷茫
最佳答案
mysql_fetch_array
只获取一行。您想多次使用它来构建一个包含整个结果集的数组:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row;
}
如果您只想要 ID,请选择 ID:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row[0];
}
关于php - mysql LIKE 三个匹配时只返回一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1332057/