Getting mysql result using php

Which one is better to retrieve data from mysql database using PHP and why?

mysql_fetch_row() mysql_fetch_assoc() mysql_fetch_array() 
+4
source share
4 answers

mysql_fetch_row () It returns an array with a numeric index / key. This is usually a quicker comparison with two other methods.

mysql_fetch_assoc () It returns an array with the column name as the key. It is a bit slower than mysql_fetch_row ().

mysql_fetch_array () It returns returns essentially two arrays. One with an associativity keyword index and one with a numerical index. mysql_fetch_array () without specifying which method you want (either MYSQL_NUM or MYSQL_ASSOC ) always returns a double array. And this is significantly more inefficient compared to mysql_fetch_row () or mysql_fetch_assoc (). You can set the result type as the second parameter.

I think, in fact, this method does not make a significant difference.

+4
source

Actually, each of them has a different way of returning data, so the question is more likely like "the one that is more suitable for your needs."

mysql_fetch_row () returns arrays like this:

 $row[0]; 

mysql_fetch_assoc () :

 $row["table_field"]; 

mysql_fetch_array () :

 $row[0]; // or $row["table_field"]; 

The “heaviest” here is perhaps fetch_array, since it takes an optional parameter to indicate whether you want your data to be returned as an associative or numeric key.

There is also this one:

mysql_fetch_object () :

 $row->table_field; 

Personally, I will use them all in accordance with my needs in each request, but if you are working with OOP in php, this last option is probably the “most enjoyable” one.

+2
source

It depends on how you want to access the row data.

  • If you want to use numeric indices use fetch_row
  • If you want to use the field name as an index, use fetch_assoc

If you don't need to use indexes (let's say you pass the string through foreach), or you don't care, using fetch_row should result in slightly less overhead (although this is very minimal). If you have no preference, prefer to use field names, as this makes it easier to read the code.

Never use fetch_array, which essentially combines fetch_row and fetch_assoc. Using both numeric and associative almost certainly makes the code less clear. (You can use fetch_array if you specify the $ result_type parameter as something other than MYSQL_BOTH, but you can also use the corresponding function, since your code will be as clear, but more concise.)

Please note: if you want to use the field name, make sure that you do not have two fields with the same name in your query. If you do this, you will need to provide an alias for this column in your query (and use it as the field name when accessing the array)

+2
source

None of them by nature surpasses the other - they are just different. The _assoc() and _row() functions are actually just handy functions for different ways of calling mysql_fetch_array() .

From the documentation

Returns an array of strings that matches the selected row, or FALSE if there are no more rows. the type of array returned depends on how the result_type parameter is defined. Using MYSQL_BOTH (by default), you will get an array with associative and number indexes. Using MYSQL_ASSOC, you only get associative indexes (how mysql_fetch_assoc () works), using MYSQL_NUM, you only get index numbers (how mysql_fetch_row () works).

0
source

All Articles