phpmysqlmysqlifetch

Can fetch_assoc() and fetch_all() be used with mysqli prepared statements?


is it possible to use the fetch_all(), fetch_assoc() etc. from the the MySQLi_Result class with a prepared statement from the MySQLi_STMT class?

I've done it using the query() method in the Mysqli class (and by escaping trough mysqli_real_escape_string()), but not using statements with binding parameters.

I'm considering using PDO.


Solution

  • First of all, if you are using PDO, you will not be using the mysqli_* function : those are two different APIs.

    Using PDO, you'll be able to use the PDOStatement->fetchAll method, if you want to fetch all results from a resultset.

    Else, you'll have to use PDOStatement->fetch, or any other fetch*() method, in a loop, to iterate over the resultset.


    If using prepared statements with MySQLi, you will apparently not have an instance of MySQLi_Result -- which means you'll have to work with mysqli_stmt::fetch over your resultset, looping by yourself.