本文介紹了mysqli fetch 數組只顯示第一行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我寫了這段代碼:
I have written this code:
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 次.為什么?我究竟做錯了什么?謝謝!
I'm trying to fetch several data using the query written in the code above, but it displays the first row 47 times. Why? What am I doing wrong? Thanks!
推薦答案
需要遍歷 MySQL 返回的結果集.這意味著為該結果集的每一行調用 mysqli_fetch_array()
.你可以使用 while
循環來做到這一點:
You need to iterate through the result set returned by MySQL. That means calling mysqli_fetch_array()
for each row of that result set. You can do that using a while
loop:
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']);
}
}
這篇關于mysqli fetch 數組只顯示第一行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!