問題描述
我在某種程度上理解 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/en/mysqli-result.fetch-array.php
如果兩列或更多列具有相同的名稱,則引用該列第一次出現的唯一方法是通過數字索引.在這些情況下,您需要 mysqli_fetch_row
或 mysqli_fetch_array
將 MYSQLI_BOTH
或 MYSQLI_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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!