2009-08-26 89 views
1

當我在我的文件search.php中查詢我的數據庫時,它只返回遇到的第一個結果。mysql LIKE在三次匹配時只返回一行

$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 

我在一個損失

回答

9

mysql_fetch_array只提取一行。你想用它幾次以建立一個數組整個結果集:

$rec = array(); 

while(($row = mysql_fetch_array($rs)) !== FALSE) { 
    $rec[] = $row; 
} 

如果你只是想編號的你要選擇的ID:

$rec = array(); 

while(($row = mysql_fetch_array($rs)) !== FALSE) { 
    $rec[] = $row[0]; 
} 
2

試試這個:

$qry = "SELECT business_id FROM business WHERE zip like '%91326%'"; 
$rs = mysql_query($qry); 
while ($rec = mysql_fetch_array($rs)) { 
    echo $session->showContents($rec); 
} 
0

這是因爲mysql_fetch_array只從結果集中提取一行。

您通常使用這樣的(從the manual):

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    printf("ID: %s Name: %s", $row[0], $row[1]); 
}