Page 1 of 1

mysql_fetch_array or mysql_fetch_assoc???

Posted: Tue Sep 24, 2002 4:31 pm
by Takuma
Which one should I use to just a get a column from the DB, which one will be the faster? Thanks :D

Posted: Tue Sep 24, 2002 4:40 pm
by mydimension
since they all share the same underlying function in the PHP source code there is no appreciable speed difference. just chose the want that suits your needs the most.

Posted: Tue Sep 24, 2002 4:50 pm
by Crashin
Is there a case where using mysql_fetch_assoc would be advantageous to using mysql_fetch_array? The latter accomplishes the same as the former and has the flexibility of numeric indices.

Posted: Tue Sep 24, 2002 7:41 pm
by hob_goblin
mysql_fetch_assoc should be slightly faster, and it will take up less RAM. though, you probably won't notice it.

Posted: Wed Sep 25, 2002 1:23 am
by Takuma
mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. This is the way mysql_fetch_array() originally worked. If you need the numeric indices as well as the associative, use mysql_fetch_array(). OK, how do you mean by a bit faster?
That's the quote from the manual but I don't really understand it, what is the difference?

Posted: Wed Sep 25, 2002 5:56 am
by twigletmac
.The difference between mysql_fetch_array() and mysql_fetch_assoc() is the indices for the arrays they return. For example if you had the following SQL statement:

Code: Select all

SELECT row1, row2, row3 FROM table
and ran it via PHP if you used mysql_fetch_assoc() to get the values for each row that array (eg. $row) would contain the following elements:

Code: Select all

$rowї'row1'], $rowї'row2'], $rowї'row3']
if you used mysql_fetch_array() it would also contain

Code: Select all

$rowї0], $rowї1], $rowї2]
Therefore, using mysql_fetch_array() you get both a numerical and associative index for the array whereas you only get an associative one when using mysql_fetch_assoc().

You can easily test this yourself by running a query and viewing the results in the array:

Code: Select all

echo '<pre>';
print_r($row);
echo '</pre>';
Mac

Posted: Wed Sep 25, 2002 9:50 am
by BDKR
From the manual:
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.
In other words, it IS slower. But there are still good reasons for using it at times.

Cheers,
BDKR

Posted: Wed Sep 25, 2002 11:29 am
by Takuma
Thanks guys, I'll continue to use mysql_fetch_array :D Looks like it's what I need and fastest :)