何时以及为何使用mysqli_fetch_row,mysqli_fetch_object,mysqli_fetch_assoc,mysqli_fetch_array [英] When and why to use mysqli_fetch_row, mysqli_fetch_object, mysqli_fetch_assoc, mysqli_fetch_array

查看:70
本文介绍了何时以及为何使用mysqli_fetch_row,mysqli_fetch_object,mysqli_fetch_assoc,mysqli_fetch_array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以某种方式理解了 mysqli_fetch_row mysqli_fetch_object mysqli_fetch_assoc mysqli_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/zh_/mysqli-result.fetch-array.php

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

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.

这篇关于何时以及为何使用mysqli_fetch_row,mysqli_fetch_object,mysqli_fetch_assoc,mysqli_fetch_array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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