本文介紹了獲取帶有 mysqli 結果的行數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要從結果對象中獲取所有行.我正在嘗試構建一個包含所有行的新數組.
I need to get all the rows from result object. I’m trying to build a new array that will hold all rows.
這是我的代碼:
$sql = new mysqli($config['host'],$config['user'],$config['pass'],$config['db_name']);
if (mysqli_connect_errno())
{
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$query = "SELECT domain FROM services";
$result = $sql->query($query);
while($row = $result->fetch_row());
{
$rows[]=$row;
}
$result->close();
$sql->close();
return $rows;
$rows
應該是包含所有行的新數組,但我得到了一個空數組.
$rows
is supposed to be the new array that contains all, rows but instead I get an empty array.
知道為什么會這樣嗎?
推薦答案
你有一個輕微的語法問題,即一個錯誤的分號.
You had a slight syntax problem, namely an errant semi-colon.
while($row = $result->fetch_row());
注意到末尾的分號了嗎?這意味著后面的塊沒有在循環中執行.擺脫它,它應該可以工作.
Notice the semi-colon at the end? It means the block following wasn't executed in a loop. Get rid of that and it should work.
另外,你可能想讓mysqli報告它遇到的所有問題:
Also, you may want to ask mysqli to report all problems it encountered:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$sql = new mysqli($config['host'], $config['user'], $config['pass'], $config['db_name']);
$query = "SELECT domain FROM services";
$result = $sql->query($query);
$rows = [];
while($row = $result->fetch_row()) {
$rows[] = $row;
}
return $rows;
這篇關于獲取帶有 mysqli 結果的行數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!