Jump to content

mysql_fetch_assoc vs mysql_fetch_array


astralaaron

Recommended Posts

mysql_fetch_array returns the results in a numeric and associative array, so there are 2 arraysthis is how you can see the difference:

$query = mysql_query('SELECT  * FROM test2');	$a = mysql_fetch_array($query );	echo "<pre>";	print_r($a);	echo "</pre>";

result:

Array(	[0] => 11	[id] => 11	[1] => Alex	[name] => Alex	[2] => Indigo	[surname] => Indigo	[3] => alex@gmail.com	[email] => alex@gmail.com)

see, there is a asociative array with keys using columun names and a numeric array, so we are fetching two arrayswhile mysql_fetch_assoc returns only an associative array

$query = mysql_query('SELECT  * FROM test2');	$a = mysql_fetch_assoc($query );	echo "<pre>";	print_r($a);	echo "</pre>";

Array(	[id] => 11	[name] => Alex	[surname] => Indigo	[email] => alex@gmail.com)

also mysql_fetch_assoc is fasterI hope it helped.

Link to comment
Share on other sites

mysql_fetch_array returns the results in a numeric and associative array, so there are 2 arraysthis is how you can see the difference:
$query = mysql_query('SELECT  * FROM test2');	$a = mysql_fetch_array($query );	echo "<pre>";	print_r($a);	echo "</pre>";

result:

Array(	[0] => 11	[id] => 11	[1] => Alex	[name] => Alex	[2] => Indigo	[surname] => Indigo	[3] => alex@gmail.com	[email] => alex@gmail.com)

see, there is a asociative array with keys using columun names and a numeric array, so we are fetching two arrayswhile mysql_fetch_assoc returns only an associative array

$query = mysql_query('SELECT  * FROM test2');	$a = mysql_fetch_assoc($query );	echo "<pre>";	print_r($a);	echo "</pre>";

Array(	[id] => 11	[name] => Alex	[surname] => Indigo	[email] => alex@gmail.com)

also mysql_fetch_assoc is fasterI hope it helped.

yes this does help, thank you.
Link to comment
Share on other sites

mysql_fetch_array($result, MYSQL_ASSOC) is same with mysql_fetch_assoc() they both return only associative arrayand mysql_fetch_array($result, MYSQL_NUM) is same with mysql_fetch_row() they return only numeric array

Link to comment
Share on other sites

Don't know about that, but if you're only gonna select one row out of a table, use mysql_fetch_assoc :)
Why? You can get multiple rows with mysql_fetch_assoc() (and the others) just use a loop..mysql_fetch_assoc( $res ) is faster (shorter) to write than mysql_fetch_array( $res, MYSQL_ASSOC );I preffered using mysql_fetch_object() or mysql_fetch_assoc(). Now I'm using a "db-wrapper" that I've written, works real smoth ;?)
Link to comment
Share on other sites

Why? You can get multiple rows with mysql_fetch_assoc() (and the others) just use a loop..mysql_fetch_assoc( $res ) is faster (shorter) to write than mysql_fetch_array( $res, MYSQL_ASSOC );I preffered using mysql_fetch_object() or mysql_fetch_assoc(). Now I'm using a "db-wrapper" that I've written, works real smoth ;?)
what do you use mysql_fetch_object() for? is that to return the field names of the table?
Link to comment
Share on other sites

mysql_fetch_object() returns the same values as mysql_fetch_assoc() etc, but as an object:

$row = mysql_fetch_assoc( $res );echo $row['id'];echo $row['otherfield'];// Or with object$row = mysql_fetch_object( $res );echo $row->id;echo $row->otherfield;

I like objects ;?)It gives a clean code (I think...)

Link to comment
Share on other sites

mysql_fetch_object() returns the same values as mysql_fetch_assoc() etc, but as an object:
$row = mysql_fetch_assoc( $res );echo $row['id'];echo $row['otherfield'];// Or with object$row = mysql_fetch_object( $res );echo $row->id;echo $row->otherfield;

I like objects ;?)It gives a clean code (I think...)

i see , thanks
Link to comment
Share on other sites

Strictly in terms of performance, mysql_fetch_row will be the fastest, but it only returns a numeric array, it does not include the column names at all. The mysql_fetch_array function is like a combination of the results of mysql_fetch_row and mysql_fetch_assoc. Both mysql_fetch_assoc and mysql_fetch_array are comparable performance-wise. The performance benefit for mysql_fetch_row isn't enough to justify using it in anything other then high-performance code. This is what the PHP manual says:

Performance: An important thing to note is that using mysql_fetch_assoc() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...