2014-10-29 50 views
0

请帮助我,我是一个初学者。我需要从我的数据库中显示多个字段。但不知何故,这个代码只显示第一行。错误显示多个字段

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 

回答

0

在你的代码是获取与$row = mysql_fetch_row($result);结果从一个记录作为一个数字数组返回只有一行。

中序获得ResultSet中的所有记录,你必须使用$row = mysql_fetch_row($result);

尝试用代码来从结果集的每个记录:

while ($row = mysql_fetch_row($result)) // while there are results 
{ 
    // get each field values inside this loop 
} 
+0

我正在使用选项/下拉表单来选择我想要显示的表,每个表中的字段值是不同的。 – Beginner 2014-10-29 07:20:28

0

您可以尝试使用

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只能在获取他们可以(分别为数字数组和关联数组)。

+0

其实我使用选项/下拉表单来选择我想要显示的表格,每个表格中的字段值是不同的。 – Beginner 2014-10-29 07:21:29

+0

是的。这可以用这个来完成。您所要做的就是根据您正在比较的值查询数据库并运行while循环。它所做的是循环遍历数据库中的每一行,以满足您的需求(在这种情况下,该表的名称)。 – FortMauris 2014-10-29 07:26:04

0
// 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