何时以及为何使用mysqli_fetch_row,mysqli_fetch_object,mysqli_fetch_assoc,mysqli_fetch_array [复制]

This question already has an answer here:

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?

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 concept, are there performance differences? Which should we use? Is differences are for better performance of for better programming habits that help developers use together for easy working?

I read "Head First PHP & MySQL (A Brain-Friendly Guide)" and "VISUAL QUICKPRO GUIDE PHP 5 ADVANCED". They use different practices but authors do not explain anything about them.

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

Thank you very much in advance!

</div>
dtot74529
dtot74529 啊,我现在明白了。但我必须关心内存的临时使用,对吧。感谢您的时间。你是个好人!
接近 7 年之前 回复
dsfds4551
dsfds4551 不会有内存泄漏。内存泄漏是分配内存但从未再次释放的问题,这是一个错误。我们在这里谈论的内容与内存泄漏无关,只是暂时使用更多内存来存储数据。这就是为什么在赠款计划中你可能永远不会注意到差异。直到你试图一次获得大量数据,你才开始注意到......
接近 7 年之前 回复
douhao2548
douhao2548 谢谢,deceze!再来一次!我是否理解正确:函数生成数组并存储在服务器内存中。这就是为什么数组中生成的数据越多,因为我的功能就会使用更多的内存。这个腰部的数据在和平的某一天可能会开始导致内存泄漏并从中消失。
接近 7 年之前 回复
dqssst0144
dqssst0144 我通常使用_fetch_assoc,因为我想通过名称访问我的数据,因为它使代码更容易理解,更不容易出错。_fetch_array返回数字和字符串索引数组,这是多余的,可能更浪费。并不是说在宏伟的计划中真的很重要,但仍然如此。做你需要的;如果你想要命名键,使用给你命名键的功能,仅此而已。
接近 7 年之前 回复
doubi12138
doubi12138 deceze,所以我可以使用我喜欢的任何东西,这无关紧要?你能告诉我最常用的功能吗?我已经习惯了mysqli_fetch_array,因为很多例子都与它有关。但为什么人们说从不使用它?或者他们没有说出任何严肃的目的?谢谢你的快速回复!
接近 7 年之前 回复
dsztc99732
dsztc99732 简单:有几种方法可以“从查询中获取结果”。您可以使用数字索引或使用列名称作为数组或对象进行索引。编写API的人意识到了这一点。他制作了几个(便利)包装来做这些事情。你可以选择自己喜欢的。这就是很多编程的方式。
接近 7 年之前 回复

3个回答



以下是一些应该回答你问题的参考文献:
mysqli_fetch_row
mysqli_fetch_assoc
mysqli_fetch_object
mysqli_fetch_array < / a> </ p>

一般情况下, PHP文档似乎 是一个很好的资源。</ p>
</ div>

展开原文

原文

Here are some references which should answer your question: mysqli_fetch_row, mysqli_fetch_assoc, mysqli_fetch_object, mysqli_fetch_array

In general the PHP Documentation seems to be a good resource for you.

ds465456465
ds465456465 您应该添加更多信息。 PHP手册不回答所有问题。 例如,本手册没有解释mysqli_fetch_assoc和mysqli_fetch_array之间的区别。 你需要做进一步的研究才能理解前者是后者的一种捷径。
接近 3 年之前 回复
dqgo99177
dqgo99177 感谢你们! 我读了参考文献,甚至在最后几周记得好几次。 我是一名设计师,现在真的很难。 但文档没有解释何时以及为什么案例和更好的用法。 我只想对自己严格要求并使用最好的概念,因为程序员经常互相嘲笑:)
接近 7 年之前 回复
dongmou5628
dongmou5628 是的,我期待像您这样的评论,但我认为这些链接将有助于OP。
接近 7 年之前 回复
drrog9853
drrog9853 这是一个糟糕的答案。 我怀疑OP已经阅读了那些,如果他没有,手册并没有真正澄清哪个更“好”。
接近 7 年之前 回复



php.net上的文档中的这些行是关键:</ p>


mysqli_fetch_array()是mysqli_fetch_row()
函数的扩展版本。 除了将数据存储在结果数组的数字索引中之外,mysqli_fetch_array()函数还可以使用结果集的字段名称作为关键字来将
数据存储在关联索引中。</ p>

</ blockquote>

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

如果两列或多列具有相同的名称,则唯一的方法 通过数字索引来引用该列的第一次出现。 在这些情况下,您需要 mysqli_fetch_row </ code>或 mysqli_fetch_array </ code>,其中 MYSQLI_BOTH </ code>或 MYSQLI_NUM </ code>作为其第二个参数(在程序用法中) 。</ p>

mysqli_fetch_assoc($ result)</ code>只是 mysqli_fetch_array($ result,MYSQLI_ASSOC)</ code>的缩写。</ p>
\ n

mysqli_fetch_object </ code>完成您的期望:它返回一行结果作为对象。 在 mysqli_fetch_assoc </ code>上使用它是一个对象或数组更好地代表正在处理的记录的问题。 对象可以是你想要的任何类 - stdClass是默认的。</ p>
</ div>

展开原文

原文

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

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

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) is just shorthand for mysqli_fetch_array($result, MYSQLI_ASSOC).

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.



我不认为有一个巨大的不同,它只需要你只需要一个函数来做同样的事情;)</ p> \ n </ div>

展开原文

原文

I dont think there is a huge differents, its only that you need only one function to do the same ;)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐