- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
que-6ren">
不太清楚为什么,但我今天已经多次看到这种情况。
global $connection;
$sql = "SELECT * FROM table";
$result = $connection->query($sql);
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
为什么不直接使用 fetch_all 的内置函数
global $connection;
$sql = "SELECT * FROM table";
$result = $connection->query($sql);
return $result->fetch_all(MYSQLI_ASSOC);
这不会使 while 循环变得不必要吗?有什么优势吗?速度差异?
最佳答案
这里已经有一些有效的陈述.. 但只是在它声明的 mysqli_fetch_all 文档中添加它 ( http://www.php.net/manual/en/mysqli-result.fetch-all.php ):
As mysqli_fetch_all() returns all the rows as an array in a single step, it may consume more memory than some similar functions such as mysqli_fetch_array(), which only returns one row at a time from the result set. Further, if you need to iterate over the result set, you will need a looping construct that will further impact performance. For these reasons mysqli_fetch_all() should only be used in those situations where the fetched result set will be sent to another layer for processing.
粗体部分暗示如果他在每次fetch_assoc之后做一些处理:
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
... //im doing stuff with $row here
}
他可以获得比使用 mysqli_fetch_all 函数(必须循环获取所有行)然后执行另一个循环来处理每一行更好的性能。
就这篇博文的一些其他速度分析而言 -> http://blog.ulf-wendel.de/2007/php-mysqli_fetch_all/有点过时(2007 年),但在比较这两种方法方面做得不错。
关于php - While循环VS。 $sqlResult -> fetch_all(MYSQLI_ASSOC) ;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055747/
我是一名优秀的程序员,十分优秀!