请在这方面帮助我,我是初学者。我需要从我的数据库中显示多个字段。但不知何故,这段代码只显示第一行。
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
发布于 2014-10-29 06:59:52
在您的代码中,您将使用$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
}
发布于 2014-10-29 07:10:01
你可以尝试使用
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相比,它们只能获取它们可以获取的内容(数字数组和关联数组)。
发布于 2014-10-29 11:40:06
// 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
https://stackoverflow.com/questions/26624362
复制相似问题