mysql_fetch_row()函数不返回整行

时间:2013-07-08 03:58:54

标签: php sql

我看到整行查询的回声:

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   echo "<li>{$row[0]}</li>";
}
echo "</ul>";  

处理查询时,数据库中只显示一列(client_id)而不是整行。但是,当我使用

print_r(mysql_fetch_assoc($result));

在while循环之前我可以看到显示:

Array (
    [client_id] => 1
    [contact_first_name] => carla
    [contact_last_name] => clausen
    [business_name] => Banana Belt Liquors
    [business_address] => 300 U.S. 24 Woodlan
    [business_address2] =>
    [business_city] => Woodland Park
    [business_state] => Colorado
    [business_zip] => 80863
    [business_areaCode_phone] => 719
    [business_phone] => 687
    [business_phone2] => 9757
    [business_url] => http://bananabeltliquors.com/
    [business_email] => info@bananabeltliquors.com
) 

3 个答案:

答案 0 :(得分:1)

mysql_fetch_row正在获取整行,但根据您的代码,您只能从整行中的第一列获取。

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   print_r($row);////// this will print the entire row
   echo "<li>{$row[0]}</li>";


    echo "<li>{$row[1]}</li>";///for other columns
    echo "<li>{$row[2]}</li>";
    ---------------------
    ----------------------
    echo "<li>{$row[8]}</li>";
}
echo "</ul>"; 

答案 1 :(得分:0)

您只是回显每个记录的第一个字段。如果您想要全部查看它们,您可以将代码更改为:

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   echo "<li>".implode("<br/>", $row)."</li>";
}
echo "</ul>";  

这将显示列表项中每行的中断所分隔的所有字段。

如果您希望每个字段都在其自己的列表元素中,则可以执行

echo "<ul>";
while($row = mysql_fetch_row($result))
{
   echo "<li>".implode("</li><li>", $row)."</li>";
}
echo "</ul>"; 

答案 2 :(得分:0)

$ row将返回整行但$ row [0]将只有第一列。所以 您可以计算列数,然后迭代$ row。

 echo "<ul>";
    while($row = mysql_fetch_row($result))
    {
       $len = count($row);
       for($i =0;$i<$len;$i++){
             echo "<li>{$row[$i]}</li>";
       }
    }
    echo "</ul>";