22

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • 10
    They both return an associative array, my question is: why use one over the other? The manual does not answer that question as far as I can see. – StudioTime Jan 26 '14 at 08:17
  • 1
    take a look at here http://www.spearheadsoftwares.com/tutorials/php-performance-benchmarking/50-mysql-fetch-assoc-vs-mysql-fetch-array-vs-mysql-fetch-object – Bhavesh G Jan 26 '14 at 08:28

4 Answers4

46

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.

Shawn31313
  • 5,978
  • 4
  • 38
  • 80
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • 1
    actually, when returning a single row, I wasn't able to use fetch array with the column name. I had to switch to fetch assoc and it istantly worked... – Liquid Core Oct 09 '16 at 19:42
  • 1
    _obviously using named indexes is far better compared to numeric indexes_ : this is a personal opinion, not a fact. Usually this is true, however there are situations where it's easier to handle numbered columns. For example if the SQL command is generated automaitcally. – luca.vercelli Feb 24 '21 at 08:55
12

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.

3

(PHP 5) From: http://php.net/manual/en/mysqli-result.fetch-array.php

mysqli_result::fetch_array -- mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both

So effectively, fetch_array() and fetch_assoc() can be essentially equivalent calls. As for performance, I recommend using whichever call that results in more readable code and only be concerned about the performance after you've clearly identified the call as a performance issue. It's more likely that you've got more serious performance issues where you least expect them to be.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
user960401
  • 31
  • 1
1

fetch_array will give you key/value pairs and with indexes, where as fetch_assoc will give you only the key/value pairs but not with indexes. For example:

//fetch_array output be like:
[name]=>'someName'
[0]=>'someName'
[email]=>'some@some.com'
[1]=>'some@some.com'

//fetch_assoc output be like

[name]=>'someName'
[email]=>'some@some.com'
nixin72
  • 322
  • 4
  • 16
Pradeepa
  • 107
  • 4