mysql_fetch_array or mysql_fetch_assoc???
Moderator: General Moderators
mysql_fetch_array or mysql_fetch_assoc???
Which one should I use to just a get a column from the DB, which one will be the faster? Thanks 
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
That's the quote from the manual but I don't really understand it, what is the difference?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?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
.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:
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:
if you used mysql_fetch_array() it would also contain
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:
Mac
Code: Select all
SELECT row1, row2, row3 FROM tableCode: Select all
$rowї'row1'], $rowї'row2'], $rowї'row3']Code: Select all
$rowї0], $rowї1], $rowї2]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>';