Below are two methods commonly used in most php codes for fetch mysql data .
mysql_fetch_array()
mysql_fetch_assoc()
This Is the result of mysql_fetch_array()
$row['fieldname'] = 'some value';
or
$row[0] = some value';
This Is the result of mysql_fetch_assoc() will only return
$row['fieldname'] = 'some value';
I prefer fetch_assoc. It returns an "associative array" (like a python dictionary). This means your array is indexed by string (in my usual case).
The default for fetch_array gives both numbered indices and associative indices.
But I never use the numbered indices, so I like to get it a tiny bit faster with fetch_assoc
The "size" of the query results don't matter in this case.
mysql_fetch_array()
generally produces overhead by returning an associative array plus indexed results.
One should decide on the type of query and the desired results on whether to use mysql_fetch_array()
or mysql_fetch_assoc()
.
If the overhead is "neglectable" and I'm sure the query succeeds and I know there's only a single result, I occasionally do:
$q = mysql_query('SELECT `column1`,`column2` FROM `table` WHERE `id` = 123');
list($column1,$column2) = mysql_fetch_array($q);
mysql_free_result($q);
For iterative proceedings (like a while
loop), this just doesn't cut it. When there's no need for "quick-extracting" results (via a list
operator) and the result has to be further processed in code, I always use mysql_fetch_assoc()
*.
* When forced to actually use the quite out-dated procedural data retrieval functions of PHP. There are alternatives.
mysql_fetch_array() vs mysql_fetch_assoc()
mysql_fetch_array(): Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. for better documentation click here! to learn more mysql_fetch_assoc(): is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
I suppose it depends on your definition of big. From that benchmark you can see that it is only before you perform upwards of 1,000,000 million fetches that you actually get a measurable difference? Is your database that big? If not, then go with what is easier for you to use (which is probably to fetch an associative array).
Another point to consider, if it is so big that there is a measurable difference then I would be getting a way from using functions like that all together. Consider using MySQLi or even better use PDO!
Well http://php.net/manual/en/function.mysql-fetch-assoc.php states
Note: 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.
So the difference shouldn't be something to worry about.