Mysqli fetch_assoc vs fetch_array

前端 未结 4 1841
心在旅途
心在旅途 2020-12-03 05:33

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 differ

相关标签:
4条回答
  • 2020-12-03 05:54

    (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.

    0 讨论(0)
  • 2020-12-03 05:55

    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.

    0 讨论(0)
  • 2020-12-03 05:58

    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'
    
    0 讨论(0)
  • 2020-12-03 06:17

    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.

    0 讨论(0)
提交回复
热议问题