【问题标题】:mysqli fetch array displays only the first rowmysqli fetch 数组只显示第一行
【发布时间】:2014-05-05 13:15:57
【问题描述】:


我写了这段代码:

require("config.php");

$rows = array();

$query = "SELECT accountname, bill_city, bill_code, bill_country,  bill_street, latitude, longitude, setype FROM vtiger_accountbillads, vtiger_account, vtiger_geocoding WHERE accountaddressid = accountid AND accountid = crmid";

$result = mysqli_query($connection, $query);

$rows_number = $result->num_rows;
echo $rows_number . "<br/><br/>";

$row = mysqli_fetch_array($result);

if($result->num_rows > 0){
    for($i=0; $i < $rows_number; $i++) {
            $rows[] = array("name" => $row[0],
                        "city" => $row[1],
                        "code" => $row[2],
                        "country" => $row[3],
                        "street" => $row[4],
                        "latitude" => $row[5],
                        "longitude" => $row[6],
                        "type" => $row[7]);
    }
}

$json = json_encode($rows);
print $json;

mysqli_free_result($row);
mysqli_close($connection);

我正在尝试使用上面代码中编写的查询获取多个数据,但它显示第一行 47 次。为什么?我究竟做错了什么?谢谢!

【问题讨论】:

  • 你应该使用 foreach 而不是 for

标签: php mysqli


【解决方案1】:

您需要遍历 MySQL 返回的结果集。这意味着为该结果集的每一行调用mysqli_fetch_array()。您可以使用while 循环来做到这一点:

while($row = mysqli_fetch_assoc($result)) {
    $rows[] = array("name"      => $row['name'],
                    "city"      => $row['bill_city'],
                    "code"      => $row['bill_code'],
                    "country"   => $row['bill_country'],
                    "street"    => $row['bill_street'],
                    "latitude"  => $row['latitude'],
                    "longitude" => $row['longitude'],
                    "type"      => $row['setype']);
    }
}

【讨论】:

  • 我已按照您的建议修改了代码,但现在它什么也没显示,我不明白为什么,因为返回的行数大于 0..
  • 如果你把var_dump($row)放在这个while循环里,你会看到什么?
  • 类似这样的:array(16) { [0]=&gt; string(10) "Google Spa" ["name"]=&gt; string(10) "Google Spa" [1]=&gt; string(10) "Vicenza" ["bill_city"]=&gt; string(10) "Vicenza" [2]=&gt; string(5) "36100" ["bill_code"]=&gt; string(5) "36100" [3]=&gt; string(6) "Italia" ["bill_country"]=&gt; string(6) "Italia" [4]=&gt; string(13) "Via Firenze 400" ["bill_street"]=&gt; string(13) "Via Firenze 400" [5]=&gt; string(9) "45.643455" ["latitude"]=&gt; string(9) "45.643455" [6]=&gt; string(9) "11.497678" ["longitude"]=&gt; string(9) "11.497678" [7]=&gt; string(15) "Geolocalization" ["setype"]=&gt; string(15) "Geolocalization" } 47 次
  • 好的,很好。这意味着您可以正常检索数据。我更新了答案以填充您的数组。让我知道这是否有任何改变。
  • 如果你在这个循环之后做var_dump($rows),你会得到日期吗?
【解决方案2】:

不要使用loop 来获取 SQL 查询。请改用while

if($result->num_rows > 0){
    while($row = mysqli_fetch_assoc($result))
    {
        $rows[] = array("name" => $row[0],
                        "city" => $row[1],
                        "code" => $row[2],
                        "country" => $row[3],
                        "street" => $row[4],
                        "latitude" => $row[5],
                        "longitude" => $row[6],
                        "type" => $row[7]);
    }
}

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 1970-01-01
    • 2014-04-24
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多