21-void
21-void

Reputation: 43

PHP/MYSQLI: fetch_assoc() vs fetch_array(MYSQLI_ASSOC)

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.

Upvotes: 2

Views: 4918

Answers (3)

Zakky
Zakky

Reputation: 139

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.

Upvotes: 1

axiac
axiac

Reputation: 72336

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.

Upvotes: 0

Goldbug
Goldbug

Reputation: 605

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

Upvotes: 0

Related Questions