Sql数组只显示第一个结果

时间:2014-12-02 18:48:09

标签: php mysql mysqli

我想单独将结果放在变量中,但为此我需要能够将它们分开调用。现在我有这个查询检索6个结果(用mysqli_num_rows测试)。 但是当我打印它时,它只显示我的6个结果中的第一行。

$result =  mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));

echo "<p>mysqli_num_rows($result)</p>";

$row = mysqli_fetch_row($result);
print_r($row);
mysqli_free_result($result);

的结果
print_r($row) = 

Array ( [0] => Iran [1] => 28 )

4 个答案:

答案 0 :(得分:2)

要获取所有行,您需要执行以下操作:

$rows = array();
while($row = mysqli_fetch_row($result)) {
   $rows[] = $row;
}
// $rows will now contain all rows in the result set

答案 1 :(得分:1)

您的函数mysqli_fetch_row()仅返回单行结果:

http://php.net/manual/en/mysqli-result.fetch-row.php

尝试像这样循环:

while ($row = mysqli_fetch_row($result) {
    // Do something
}

谢谢,

安德鲁

答案 2 :(得分:0)

你应该使用

    while ($row = mysqli_fetch_row($result)){
print_r($row);
//do more stuff...
}

您可以使用fetch_assoc而不是fetch_row来拥有逻辑数组键

答案 3 :(得分:0)

试试这个会起作用:

$result =  mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));

echo "<p>mysqli_num_rows($result)</p>";

$rows = array();
$row = mysqli_fetch_row($result);

do
{
   $rows[] = $row;

}while($row = mysqli_fetch_row($result))

mysqli_free_result($result);