Skip to content
Advertisement

Uncaught Error: Call to undefined method mysqli_stmt::fetchAll()

I have following lines of code to fetch multiple records using PHP 7.3

$query = "Select * from tblorders";         
$stmt = $connection->prepare($query);

$stmt->execute();           
$result = $stmt->fetchAll();

The last line issues as error.

Error Details

Uncaught Error: Call to undefined method mysqli_stmt::fetchAll()

I can confirm that the connection is not null and has proper connection details. Am I missing anything?

Advertisement

Answer

This is because there is no such function! You are mixing PDO and mysqli.

If you want to fetch all records from a mysqli prepared statement you need to do it in two steps. First, fetch the result set using mysqli_stmt::get_result() and then use mysqli_result::fetch_all()

$query = "Select * from tblorders";
$stmt = $connection->prepare($query);
$stmt->execute();

$resultSet = $stmt->get_result();
$data = $resultSet->fetch_all(MYSQLI_ASSOC);

However, I would strongly advise learning PDO instead of mysqli as it is much easier and offers more options.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement