1

I have been reading about mysql_fetch_* methods. This is what I have learnt from PHP.org website.

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc — Fetch a result row as an associative array
mysql_fetch_object — Fetch a result row as an object
mysql_fetch_row — Get a result row as an enumerated array

It looks like mysql_fetch_array contains all the values that are present in

mysql_fetch_assoc,mysql_fetch_object,mysql_fetch_row. Because mysql_fetch_assoc contains only Associative array,

mysql_fetch_row contains data in Numeric Array.

mysql_fetch_object also returns associative array.

Kindly tell me whether my understanding is correct or wrong.

2
  • You are correct except for the mysql_fetch_object part. mysql_fetch_object returns an object with column names being the properties of that object. This type of thing is not available from mysql_fetch_array Commented Sep 4, 2013 at 8:00
  • 5
    Yes, you're basically on the right track with that. However, please note that all the mysql_xxx() functions are deprecated and not recommended for use; if you're learning PHP, you should be learning a more modern API such as PDO. Commented Sep 4, 2013 at 8:01

1 Answer 1

4

mysql_fetch_assoc returns an associative array, mysql_fetch_row returns a numeric array, and with mysql_fetch_array you can chose what would be the output. This function accepts an optional parameter which can take values:

  • MYSQL_ASSOC - returns associative array
  • MYSQL_NUM - returns numeric array
  • MYSQL_BOTH - returns combined numeric and associative array

The last value is default.

mysql_fetch_object is slightly different as it returns an object wich has fields corresponding to columns in result fetched from database.

As a sidenote I would like to add that mysql_* functions are deprecated and you should switch to mysqli or PDO.

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

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.