PHP/MYSQLI: fetch_assoc() vs fetch_array(MYSQLI_AS

2019-08-26 14:18发布

问题:

Is there any difference of any kind between $result->fetch_assoc() and
$result->fetch_array(MYSQLI_ASSOC) or they are exactly the same thing?

I have searched a bit before making this question but the only thing I've found (here) is that $result->fetch_array() with no params allows numeric and associative indexes while
$result->fetch_assoc() only allows the associative indexes and therefore the last one has a better performance.

回答1:

fetch_array() is used when you need access to both associative and numeric indexes.

Use fetch_assoc() when you only need associative indexes.

Php.net docs say

Fetch a result row as an associative, a numeric array, or both

Php.net fetch_array() description



回答2:

Have you read the documentation of mysqli_result::fetch_assoc() and mysqli_result::fetch_array()?

The last one explains the possible values for its argument:

$resulttype

This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH.

By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both.



回答3:

Yes, purpose and returned formats.

fetch_array() has more output formats. You can see here PHP Manual : mysqli_result::fetch_array.

Whereas PHP Manual : mysqli_result::fetch_assoc() outputs a single format.



标签: php mysqli