11

This post gives four ways of retrieving the result of a MySQL query:

  • mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both

    $row = mysqli_fetch_array($result);
    echo $row[0]; // or
    echo $row['online'];
    
  • mysqli_fetch_assoc — Fetch a result row as an associative array

    $row = mysqli_fetch_assoc($result);
    echo $row['online'];
    
  • mysqli_fetch_object — Returns the current row of a result set as an object

    $row = mysqli_fetch_object($result);
    echo $row->online;
    
  • mysqli_fetch_row — Get a result row as an enumerated array

    $row = mysqli_fetch_row($result);
    echo $row[0];
    

Is there any significant difference between these four functions, in terms of either performance or functionality, or can they be used interchangeably?

Community
  • 1
  • 1
1''
  • 26,823
  • 32
  • 143
  • 200
  • Well you could benchmark the performance yourself using PHP but in general the performance would be pretty much the same and perhaps on feth_array slight bigger. – Prix Sep 02 '13 at 16:57
  • @Prix That's a fair point about benchmarking myself, but I expect the relative performance would differ between different types of queries. Also, I'd like to confirm that they actually behave the same in all situations. – 1'' Sep 02 '13 at 16:59
  • Well I don't think it would differ as much because all the data mysqli library would need, is available from the fields name to either create the object or associative array. Then again each different query will give you a different performance result. – Prix Sep 02 '13 at 17:00
  • @Prix It makes intuitive sense that the only overhead is the actual array creation or object construction. Thanks for your help. – 1'' Sep 02 '13 at 17:06

1 Answers1

14

Is there any significant difference between these four functions

No.

can they be used interchangeably?

Yes.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345