mysql_fetch_array or mysql_fetch_assoc???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

mysql_fetch_array or mysql_fetch_assoc???

Post by Takuma »

Which one should I use to just a get a column from the DB, which one will be the faster? Thanks :D
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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.
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

mysql_fetch_assoc should be slightly faster, and it will take up less RAM. though, you probably won't notice it.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Thanks guys, I'll continue to use mysql_fetch_array :D Looks like it's what I need and fastest :)
Post Reply