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)
source share