像多维数组一样从mysqli_fetch_array读取数据吗? [英] Read data from mysqli_fetch_array like a multidimensional array?
问题描述
当我使用mysqli_fetch_array()
时,我得到一个数组,但是如何读取值?是While-Loop的唯一选择,还是我可以从行和列中选择任何值,例如具有诸如row[0] column [3]
的索引的多维数组?
When I use mysqli_fetch_array()
I get an array, but how do I read the values? Is a While-Loop the only option, or can I pick any value from a row and column like a multidimensional array with index like row[0] column [3]
?
推荐答案
while
循环在每次迭代中获取一行.
while
loop fetches you a row per iteration.
您可以使用 mysqli_fetch_all
You can get all rows as multidimensional array with mysqli_fetch_all
之后,您可以使用[rowNum][colNum]
但是请注意,当结果中有很多行时-数组可能很大,并且会导致内存或其他问题.
But beware when your result has lot of rows - array can be very big and cause memory or other issues.
更新以进行澄清:
如果要接收多维的行数组,则有两种方式:
if you want to receive multidimensional array of rows there are two ways:
首先:使用fetch_assoc
/fetch_array
遍历mysqli_result并将行附加到数组:
First: iterate over mysqli_result with fetch_assoc
/fetch_array
and append row to array:
$rows = array();
while ($row = mysqli_fetch_array($res)) {
$rows[] = $row;
}
echo $rows[0]['whatever'];
第二步:通过一个函数调用即可接收所有结果:
Second: receive all results with one function call:
$rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
echo $rows[0]['whatever'];
仅此而已.没有更多可用的方法.
That's all. No more methods are available.
这篇关于像多维数组一样从mysqli_fetch_array读取数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!