Search code examples
phpmysqlifetchresultset

mysqli_fetch_array($result, MYSQLI_ASSOC) vs $result->fetch_assoc()


I wonder what's the difference between these two ways to extract data from a result, which one is the faster and more modern cause both of them work with mysqli.

Thanks for your patience.


Solution

  • mysqli has two different APIs for access. One, is a purely functional interface, using mysqli_* functions, and is somewhat similar to the interface used by the older mysql extension. The other API is an OOP interface.

    In practical effect, both interfaces accomplish exactly the same thing. Assuming $db is a mysqli object retrieved from either mysqli_connect or new mysqli, these two lines mean exactly the same thing:

     $db->query('query string');
     mysqli_query($db, 'query string');
    

    and in fact, at an engine level, are basically aliases of each other. Neither one is going to be faster than the other.

    You should use whichever one is most comfortable to you, though OOP style is generally more widespread, and both work equally well.