0

我看到一整行查询的回声:

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
) 
4

3 回答 3

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>"; 
于 2013-07-08T04:04:07.150 回答
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>";  
于 2013-07-08T04:14:32.060 回答
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>"; 
于 2013-07-08T04:05:32.750 回答