何时以及为何使用 mysqli_fetch_row、mysqli_fetch_object、mysqli_fetch_assoc、mysqli_fetch_array

2021-12-25 00:00:00 fetch php mysql mysqli

我在某种程度上理解 mysqli_fetch_rowmysqli_fetch_objectmysqli_fetch_assocmysqli_fetch_array 之间的区别.

I understand in some way the differences between mysqli_fetch_row, mysqli_fetch_object, mysqli_fetch_assoc and mysqli_fetch_array.

我的问题是它们是否如此相似(如果它们真的像许多主题所说的几乎相同)我们应该使用哪个?为什么我们要使用首选的,是否有一些性能差异?

My question is if they are so similar (if they are really almost the same as many topics say) which should we use? Why should we use the preferred one and is there some performance difference?

我读过一些人说永远不要使用 mysqli_fetch_array.我很迷惑.我现在正在阅读一些 PHP 书籍,所有示例都包含它.如果这是不正确的,我在锻炼时停止使用它并使用您可以解释和推荐的其他东西更好吗?

I have read some people say never to use mysqli_fetch_array. I am confused. I am reading now some PHP books and all the examples include it. If this is incorrect is it better for me to stop using it while exercising and use something else that you could explain and recommend?

我也读到这些是相等的:

I have also read that these are equal:

mysqli_fetch_array( $result, MYSQLI_ASSOC ) = mysqli_fetch_assoc( $result ) 
mysqli_fetch_array( $result, MYSQLI_NUM ) = mysqli_fetch_row( $result )
mysqli_fetch_array ( $result ) = mysqli_fetch_assoc( $result ) + mysqli_fetch_row( $result )

如果它们在概念上是相同的,是否存在性能差异?我们应该使用哪个?差异是出于性能原因还是为了开发人员的方便?

If they are equal as a concept, are there performance differences? Which should we use? Are the differences because of performance reasons or for the developer's convenience?

我坚信这些差异有很大的原因,它们有不同的用法和案例,但我找不到在哪里使用不同的函数和语法.

I strongly believe that these differences have a big reason and they have different usage and cases, but I cannot find where to use different functions and syntax.

推荐答案

php.net 文档中的这些行是关键:

These lines from the documentation on php.net are key:

mysqli_fetch_array() 是 mysqli_fetch_row() 的扩展版本功能.除了将数据存储在数字索引中结果数组,mysqli_fetch_array() 函数也可以存储关联索引中的数据,使用结果集的字段名称作为钥匙.

mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.

http://www.php.net/manual/en/mysqli-result.fetch-array.php

如果两列或更多列具有相同的名称,则引用该列第一次出现的唯一方法是通过数字索引.在这些情况下,您需要 mysqli_fetch_rowmysqli_fetch_arrayMYSQLI_BOTHMYSQLI_NUM 作为第二个参数(在程序使用中).

In cases where two or more columns have the same name the only way to reference the first occurence(s) of that column is by numerical index. In these cases you need mysqli_fetch_row or mysqli_fetch_array with either MYSQLI_BOTH or MYSQLI_NUM as its second argument (in procedural usage).

mysqli_fetch_assoc($result) 只是 mysqli_fetch_array($result, MYSQLI_ASSOC) 的简写.

mysqli_fetch_object 符合您的预期:它将一行结果作为对象返回.在 mysqli_fetch_assoc 上使用这个是一个对象或数组是否更好地代表正在处理的记录的问题.该对象可以是您想要的任何类 - stdClass 是默认值.

mysqli_fetch_object does what you expect: It returns a row of results as an object. Use of this over mysqli_fetch_assoc is a matter of whether an object or an array better represents the record being handled. The object can be of whatever class you want - stdClass is the default.

相关文章