何时以及为何使用 mysqli_fetch_row、mysqli_fetch_object、mysqli_fetch_a

When and why to use mysqli_fetch_row, mysqli_fetch_object, mysqli_fetch_assoc, mysqli_fetch_array(何时以及为何使用 mysqli_fetch_row、mysqli_fetch_object、mysqli_fetch_assoc、mysqli_fetch_array)
本文介绍了何时以及为何使用 mysqli_fetch_row、mysqli_fetch_object、mysqli_fetch_assoc、mysqli_fetch_array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在某种程度上理解 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)