Happy Day..

Hello All!

I want to know the Difference between mysql_fetch_array and mysql_fetch_assoc.,and Im new here.. and also to php.. Plz help me to solve my problems..

Recommended Answers

All 4 Replies

mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter and it doesn't really need to exist because it just returns the key names instead of Numeric keys and key names which happens in mysql_fetch_array.'Associative arrays' returned by mysql_fetch_assoc() are arrays with key names so , they're more human readable.Sometimes mysql_fetch_assoc() is faster than mysql_fetch_array(). mysql_fetch_assoc() may also need less memory as it generates an array of key names instead of keynames and key indexes in mysql_fetch_array().

Member Avatar for P0lT10n

Difference: mysql_fetch_assoc() will always assing a non-secuencial key (like "color" and not a number). mysql_fetch_array() will assing a number key if there are not "word" key (0 and not "color" if there are not "color") but, you can choose to assing of key with a parameter...

See PHP Documentation...

while using the mysql_fetch_array('query here'); you dont need to know the field name in advance .But if you use mysql_fetch_assoc('query here'); you must know the field name in advance.
exampe:

$rows = mysql_fetch_array( "select name, address from people");

Means you then get each of the row results as a straight array, meaning you dont necessarily need to know in advance the order elements arrive via the select.

foreach( $rows as $row )
echo $row[0] . ' lives at ' . $row[1] ;

$rows = mysql_fetch_assoc( "select name, address from people" );

Means you then get each of the row results as a named array elements, an associative array.

foreach( $rows as $row )
echo $row['name'] . ' lives at ' . $row['address'];

mysql_fetch_array:

Mysql_fetch_array() returns row as an associative, numeric array or both based on parameters passed in it. Three types of parameters can be passed in mysql_fetch_array function.
      1. MYSQL_BOTH - return both associative and numeric array
      2. MYSQL_ASSOC - return associative array
      3. MYSQL_NUM - return numeric array

mysql_fetch_assoc():

       It  return row as an associative array. Here, we can use numeric indices to get values from mysql data using php.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.