3 回答

TA貢獻1839條經(jīng)驗 獲得超15個贊
在您的情況下,由于您正在使用mysqli_fetch_assoc()它將遍歷表字段的所有行,并且$row如果您將其放入while循環(huán)中,該變量將具有與當(dāng)前行關(guān)聯(lián)的數(shù)組。如果您想在while循環(huán)之外訪問它,您可以使用您在上面創(chuàng)建另一個數(shù)組的方法。在這種情況下,您仍然需要按名稱引用該字段:
<?php
$query = "SELECT * FROM `table`";
$result = mysqli_query($link, $query);
$Array = array();
?>
<pre>
<?php
while($row = mysqli_fetch_assoc($result)) {
$Array[] = $row;
}
print_r($Array[1]['your_field']);
?>
</pre>

TA貢獻1788條經(jīng)驗 獲得超4個贊
其他人已經(jīng)說過,如果您使用mysqli_fetch_assoc()
.
如果您使用mysqli_fetch_row($result)或使用mysqli_fetch_array($result, MYSQLI_NUM) ,則可以獲得整數(shù)索引數(shù)組。
如果您使用mysqli_fetch_all($result, MYSQLI_NUM) ,您還可以簡化代碼,這樣您就不必編寫自己的 while 循環(huán)。就像您描述的那樣,該函數(shù)返回一個數(shù)組數(shù)組。

TA貢獻1790條經(jīng)驗 獲得超9個贊
您可以簡單地使用fetch_all(). 例如:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli('localhost', 'username', 'password', 'databaseName');
$link->set_charset('utf8mb4'); // always set the charset
$query = "SELECT * FROM `table` ";
$result = $link->query($query); // If variables are involved you must use prepare/bind_param/execute
$matrixOfRowsAndCols = $result->fetch_all();
echo $matrixOfRowsAndCols[1][1];
默認獲取樣式是數(shù)字數(shù)組,因此您將獲得數(shù)字列索引以及數(shù)字行索引。不需要任何循環(huán)。
- 3 回答
- 0 關(guān)注
- 340 瀏覽
添加回答
舉報