请帮帮我,我是初学者。我需要显示数据库中的多个字段。但不知何故,此代码仅显示第一行。
if(mysql_num_rows($result)>0)
{
$output .= "<p>Table: $tbl</p><p>";
// BUG - SHOULD LOOP THRU MULTIPLE rows
$row = mysql_fetch_row($result);
$i = 0;
while ($i < mysql_num_fields($result))
{
//echo "Information for column $i:<br />'n";
$fieldN = mysql_field_name($result, $i);
if (!$fieldN) { $output .= "No info available<br />'n"; }
if ($row[$i]) {
$fieldN = mysql_field_name($result, $i);
if (!$fieldN) { $fieldN = "No_field_name"; }
$output .= " $tbl F: $fieldN V: $row[$i]<br />'n";
}
$i++;
} // end while
$output .= "</p>";
} // end if number of rows > 0
在代码中,您正在获取带有 $row = mysql_fetch_row($result);
的结果,该结果仅以数字数组的形式返回记录集中的一行。
为了获取结果集中的所有记录,必须使用 $row = mysql_fetch_row($result);
从结果集中获取每条记录
尝试使用代码:
while ($row = mysql_fetch_row($result)) // while there are results
{
// get each field values inside this loop
}
您可以尝试使用
while ($row = mysqli_fetch_array($result)) {
而不是
while ($i < mysql_num_fields($result))
mysqli_fetch_array(( 允许您将数据作为数字数组和关联数组获取。
如果你有一个名为 ID 的数据库字段,它位于数据库表的第一行,你可以使用如下的东西:
$id = $row[0];
$id = $row['ID'];
与只能获取数字数组和关联数组(分别为数字数组和关联数组(的 fetch_row 和 fetch_assoc 相比。
// LOOP THRU MULTIPLE rows
while ($row = mysql_fetch_row($result)) {
$i = 0;
while ($i < mysql_num_fields($result)) {
//echo "Information for column $i:<br />'n";
if ($row[$i]) {
$fieldN = mysql_field_name($result, $i);
if (!$fieldN) { $fieldN = "No_field_name"; }
$output .= " $tbl F: $fieldN V: $row[$i]<br />'n";
}
$i++;
} // end while loop thru fields in each row
$output .= "</p>";
} // end while there is a row