2013-06-29 58 views
2
mysql_connect('localhost:3036', 'root', 'xxxx'); 

mysql_select_db('extractor'); 

$query = mysql_query("SELECT trackingno FROM xx where orderid='".$item->increment_id."'"); 

$compiledresults = mysql_fetch_array($query); 

foreach($compiledresults as $items){ 

echo $items."</br>"; 
} 

它总是只返回两个值。任何想法为什么?mysql只返回2个结果

+5

您确定有超过2个符合您指定条件的结果吗? –

+2

我想你应该使用'while($ compiledresults = mysql_fetch_assoc($ query))'而不是从'mysql_fetch_assoc($ query)'得到第一个结果,因此[documentation](http://php.net/ manual/en/function.mysql-fetch-array.php)说_Returns一个数组,对应于获取的行并将内部数据指针提前移动.. ..请注意_deprecation warning_红色。 – dbf

+0

@dbf建议的循环更好,因为如果结果是巨大的,一次枚举它是一个巨大的内存用户,而一次列举一个是和平的。它也将为您节省6-8周的调试 –

回答

1

只是修复您的代码:

$query = mysql_query("SELECT trackingno FROM xx where orderid='".$item->increment_id."'"); 
if ($query){ 
    while ($data = mysql_fetch_assoc($query)){ 
    echo $data['trackingno'] ; 
    } 
} 

的原因,你得到2项是您用来mysql_fetch_array一次。这给你从数据库中只有一行。数组中的第一个元素是数字索引的,另一个是字符串索引的。

所以你有:$compiledresults[0]$compiledresults['trackingno']其实。