1

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
21-void
  • 33
  • 1
  • 3
  • They're the same, if you have the `MYSQLI_ASSOC` argument to the `fetch_array()` function. If you don't have that particular argument, they're different. – Qirel Jul 18 '17 at 21:38
  • If you use the `MYSQLI_ASSOC` constant with `fetch_array` it is _exactly the same_ as `fetch_assoc`. They both call the same internal function. They are essentially aliases. I would expect to see negligible if any difference in performance. – Don't Panic Jul 18 '17 at 22:04

3 Answers3

1

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.

Zakky
  • 139
  • 6
  • Why do you thing `fetch_array()` is slower? – axiac Jul 18 '17 at 21:55
  • You know, i have nothing to back that up. My logic is that is most likely a larger code block with more logic to process. However, probably aliases, as mentioned by @DontPanic. So, i'll strike it from the answer. In essence, PHP could depreciate `fetch_assoc` and `fetch_row` in favor of `fetch_array` to consolidate the code. – Zakky Jul 18 '17 at 22:02
  • 1
    Well, I guess `fetch_array()` takes more time than `fetch_assoc()` or `fetch_row()` to produce the array it returns (if we consider the case when `$resulttype` is `MYSQLI_BOTH`, it puts each value twice in the generated array). While in absolute numbers, `fetch_array()` is indeed slower, all three functions probably need 1/1000 of the time needed by the query to execute. Choosing between `fetch_array()` and `fetch_assoc()` is not a source of performance improvement. – axiac Jul 18 '17 at 22:14
  • @axiac agreed :) – Zakky Jul 18 '17 at 22:29
  • Buddy, you will get more upvotes if your answers are more complete, adding links is not enough, include more explanations in case the links stop working, and add examples with its resulting data. – Jose Manuel Abarca Rodríguez Sep 03 '21 at 20:35
0

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

Goldbug
  • 605
  • 6
  • 8
0

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.

axiac
  • 68,258
  • 9
  • 99
  • 134