mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object

The functions are all very similar:

mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object()

I have recently started using mysql_fetch_object as I am doing alot more OOP with PHP.

But what are peoples opinions on which one is best to use and why, and maybe which scenario they are best to be used in.

Thanks for your thoughts!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

mysql_fetch_array will get you an array that can have as keys :

  • both numbers and names of columns, if using MYSQL_BOTH
  • columns names, using MYSQL_ASSOC — in this case, you’ll get the same thing you get when using mysql_fetch_assoc
  • only numbers (depending on the order of columns in the query), if using MYSQL_NUM

Getting results indexed by columns names is probably the most useful solution — easier to use, at least.

But getting results indexed by the positions of the fields in the select clause is interesting in one situtation : when you have several columns that have the same name or alias.

In this case, as you cannot have two entries with the same index in an array, you will be able to access only one of those columns using the column name as index.

For the other columns that have the same name, you’ll have to use numeric indexes.

That situation is probably the only case for which I would use mysql_fetch_array — and I rather prefer using aliases in my query, to avoid that situation — it’s more clear, in my opinion.

mysql_fetch_assoc will get you an array, with columns names as keys, and data as values.

Not much to say, actually.

And mysql_fetch_object will get you objetcs in return.

Choosing between mysql_fetch_assoc and mysql_fetch_object most probably depend on how you develop your application : if using objects everywhere, the second one is probably the most suited.

If using arrays as data-containers, you can just go with the first one.

Method 2

mysql_fetch_array() fetches a result row as an associative array, a numeric array, or both.
It returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how $result_type is defined.

By using MYSQL_NUM, you only get number indices (as $row[0], $row1, etc) i.e., numeric array.

By using MYSQL_ASSOC, you only get associative indices (as $row[“id”], $row[“name”], etc) i.e., associative array.

By using MYSQL_BOTH (default), you’ll get an array with both associative and number indices. (as $row[0], $row[“name”], etc) i.e., both numeric array and associative array.

mysql_fetch_assoc() fetches a result row as an associative array. (column names as key).

mysql_fetch_object() fetches the result row as an object.

It returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.

To me there are adantages of using
mysql_fetch_assoc() in that you can use the array functions such as
array_walk and uasort().

Method 3

Suppose we have following table

Name      CountryCode
-----     ----------
Bangladesh  BD
Pueblo     USA
Arvada     USA

$query = “SELECT Name, CountryCode FROM City”;

$result = mysqli_query($connection, $query)

  1. mysqli_fetch_object (data can be fetch as object)
     while ($obj = mysqli_fetch_object($result)) {
        printf ("%s (%s)n", $obj->Name, $obj->CountryCode);
     }
  2. mysqli_fetch_assoc (fetch as associative array i.e. key)

    while ($row = mysqli_fetch_assoc($result)) {
    printf ("%s (%s)n", $row["Name"], $row["CountryCode"]);
    }

  3. mysqli_fetch_array (numeric & associative both)

    while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    printf("%s (%s)n", $row[0], $row[1]);
    }

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    printf("%s (%s)n", $row["Name"], $row["CountryCode"]);
    }

Method 4

Short description:

mysql_fetch_assoc() : This gets you an associative array of data.

mysql_fetch_array() : This returns a combination array of associative elements as well as data with numerical index.

mysql_fetch_object() : Returns an object with properties that correspond to the fetched row.

Source


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x