Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysqli fetch_assoc vs fetch_array

Tags:

When I'm returning one row from a table, to gather the results I usually use e.g.:

$info = $result->fetch_assoc();  

What is the difference between that and:

$info = $result->fetch_array(); 

Is there a reason to use one over the other when returning only one row, or is it just personal preference?

like image 529
StudioTime Avatar asked Jan 26 '14 08:01

StudioTime


People also ask

What is the difference between Fetch_assoc and Fetch_array?

fetch_array returns value with indexing. But Fetch_assoc just returns the the value. here array location 0 contains 11 also this location name is 'id'. means just returns the value.

What does Fetch_assoc mean?

The fetch_assoc() / mysqli_fetch_assoc() function fetches a result row as an associative array.

What is Fetch_array () function?

The fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both. Note: Fieldnames returned from this function are case-sensitive.

What is difference between Mysqli_fetch_array () and mysqli_fetch_assoc () function?

The major difference between mysqli_fetch_assoc and mysqli_fetch_array is the output format of result data. mysqli_fetch_assoc returns data in an associative array and mysqli_fetch_array returns data in a numeric array and/or in an associative array.


2 Answers

It's all about performance

fetch_array() returns one array with both numeric keys, and associative strings (column names), so here you can either use $row['column_name'] or $row[0]

Where as fetch_assoc() will return string indexed key array and no numeric array so you won't have an option here of using numeric keys like $row[0].

So the latter one is better in performance compared to fetch_array() and obviously using named indexes is far better compared to numeric indexes.

like image 121
Mr. Alien Avatar answered Sep 22 '22 22:09

Mr. Alien


fetch_array returns value with indexing. But Fetch_assoc just returns the the value.

for example fetch_array returns

[0] => 11 [id] => 11 [1] => 2014-12-29 [date] => 2014-12-29 

here array location 0 contains 11 also this location name is 'id'.

same things fetch_assoc will returns

[id] => 11 [date] => 2014-12-29 

means just returns the value.

like image 44
Mohammad Shahnewaz Sarker Avatar answered Sep 18 '22 22:09

Mohammad Shahnewaz Sarker