問(wèn)題描述
我正在嘗試使用以下代碼(在 $host 中使用適當(dāng)?shù)闹档?對(duì)小型 mysql 數(shù)據(jù)庫(kù)進(jìn)行非常非常簡(jiǎn)單的查詢(xún):
I'm trying to make a very, very simple query of a small mysql database, using the following code (with appropriate values in $host, etc.):
$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result);
echo print_r($result);
echo '<br><br>';
echo print_r($row);
如您所見(jiàn),我以人類(lèi)可讀的方式打印了結(jié)果,結(jié)果是:
As you can see, I printed out the results in a human-readable way, yielding:
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => Array ( [0] => 19 ) [num_rows] => 9 [type] => 0 ) 1
Array ( [0] => Arizona State Univ. [university] => Arizona State Univ. ) 1
該專(zhuān)欄中有一些示例大學(xué),所以我不確定我做錯(cuò)了什么.
There are a few example universities in that column, so I'm not sure what I'm doing wrong.
推薦答案
mysqli_fetch_array 每次調(diào)用時(shí)都通過(guò)指針工作
mysqli_fetch_array works by pointers each time it's called
想象以下內(nèi)容
$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result); // this is the first row
$row = mysqli_fetch_array($result); // now it's the second row
$row = mysqli_fetch_array($result); // third row
要以您想要的方式實(shí)際顯示數(shù)據(jù),我建議您執(zhí)行以下操作
To actually display the data the way you want it to, I suggest you do the following
$rows = array();
$result = mysqli_query($connection, "select university from universities_alpha");
while($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
print_r($rows);
這篇關(guān)于mysqli_fetch_array 只返回一個(gè)結(jié)果的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!