11

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?

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Liam James
  • 414
  • 1
  • 6
  • 15
  • 3
    Simple: There are several ways to "get results from a query". You can get them indexed numerically or using their column names and as array or as object. The guy who wrote the API realised this. He made several (convenience) wrappers to do these things. You can choose which you like. That's how a lot of programming goes. – deceze Nov 28 '13 at 08:31
  • 1
    deceze, so I can use whatever I like and this does no matter? Can you suggest me the most used function? I am getting used to with mysqli_fetch_array because lot of examples were with it. But why people say never use it? Or they do not say this with any serious purpose? And Thank you for your fast reply! – Liam James Nov 28 '13 at 08:45
  • 1
    I'd typically use `_fetch_assoc`, because I want to be accessing my data by name because it makes the code more comprehensible and less prone to errors. `_fetch_array` returns numerically *and* string indexed arrays, which is superfluous and potentially maybe possibly more wasteful. Not that it really matters in the grand scheme of things, but still. Do what you need; if you want named keys, use the function that gives you named keys and nothing more. – deceze Nov 28 '13 at 08:49
  • 1
    Thank you, deceze! Once again! Do I understand correctly: The functions generate arrays and they are stored in the server memory. That is why the more generated data in the arrays, the more memory will be used because of my functions. And this waisted data one peace after peace some day may start causing memory leaks and out of it. – Liam James Nov 28 '13 at 08:56
  • 1
    There will be no memory leaks. Memory leaks are problems where memory is allocated but then never released again, it's a bug. What we're talking about here has nothing to do with memory leaks, just with *temporarily* using more memory to store the data. That's why in the grant scheme of things you'll probably never notice the difference. Until you are trying to get a lot of data at once and you *do* start to notice... – deceze Nov 28 '13 at 08:58
  • Ah, I understand now. But I have to care about the temporary usage of memory, right. Thank you for your time. You are great guy! – Liam James Nov 28 '13 at 09:03

2 Answers2

6

These lines from the documentation on php.net are key:

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

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) is just shorthand for mysqli_fetch_array($result, MYSQLI_ASSOC).

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.

tvanc
  • 3,807
  • 3
  • 25
  • 40
1

I dont think there is a huge differents, its only that you need only one function to do the same ;)

Jacob A.
  • 270
  • 3
  • 10