2

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.

2
  • 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. Commented Jul 18, 2017 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. Commented Jul 18, 2017 at 22:04

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Why do you thing fetch_array() is slower?
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.
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 agreed :)
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.
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.