mysqli_fetch_array()还是mysqli_fetch_all()? [英] mysqli_fetch_array() or mysqli_fetch_all()?

查看:113
本文介绍了mysqli_fetch_array()还是mysqli_fetch_all()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到mysqli_fetch_array()mysqli_fetch_all()之间的比较表明,使用mysqli_fetch_all()会占用更多的内存,并且我必须遍历数组.

I see comparisons between mysqli_fetch_array() and mysqli_fetch_all() that say that with mysqli_fetch_all() it will take more memory and I will have to iterate over the array.

mysqli_fetch_all()是对mysqli的一个呼叫,但mysqli_fetch_array()是每行一个呼叫,例如100个呼叫.

mysqli_fetch_all() is one call to mysqli but mysqli_fetch_array() is one call per row, for example 100 calls.

我不知道mysqli处理的工作原理:同时考虑通话次数,调用mysqli_fetch_array()真的更有效率吗?

I don't know how the mysqli processing works: is calling mysqli_fetch_array() really more efficient when you also take the number of calls into account?

(我已经知道返回的数据可以是关联数组,也可以不是关联数组)

(I already understand that the returned data can be associative arrays or not)

推荐答案

效率无关.仅仅是关于可用性.

fetch_all()是被称为语法糖"的东西-一种使经常执行的操作自动化的简写.它可以很容易地实现为用户级功能:

fetch_all() is a thing that is called a "syntax sugar" - a shorthand to automate a frequently performed operation. It can be easily implemented as a userland function:

function mysqli_fetch_all ($resouce, $mode = MYSQLI_BOTH)
{
    $ret = [];
    while ($row = $resource->fetch_array($mode))
    {
        $ret[] = $row;
    }
    return $ret;
}

因此,您可以分辨出这些功能的用例:

Thus you can tell use cases for these functions:

    如果需要一个数组,则必须使用
  • fetch_all(),该数组包含所有返回的行,这些行将在其他地方使用.
  • 如果要就地一一处理所有行,则必须使用循环中的
  • fetch_assoc().
  • fetch_all() have to be used if you need an array, consists of all the returned rows, that will be used elsewhere.
  • fetch_assoc() in a loop have to be used if you're going to process all the rows one by one right in place.

就这么简单.

这些函数有不同的用途,因此比较它们是没有意义的.

These functions bear different purpose and thus there is no point in comparing them.

请注意,就语法糖而言,PDO的甜度提高了十倍,因为它的fetchAll()函数可以返回数十个数据不同的格式

Note that PDO is tenfold more sweet in terms of syntax sugar, as it's fetchAll() function can return data in dozens different formats

这篇关于mysqli_fetch_array()还是mysqli_fetch_all()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆