Get result set from a prepared statement using MySQLi in PHP

I had to go over this dozens of times before I found out a decent solution. When you use a prepared statement it’s a bit of a mess trying to get an array of results from the result akin to $result->fetch_all(); So here’s what I found:


$query = $mysqli->prepare("SELECT * FROM `table`");

$query->execute();

$result = $query->get_result();

$resultArray = $result->fetch_assoc();

Use the get_result() method to get a result object from the statement object and then you can use the typical methods associated with the MySQLi result object.

See: http://uk.php.net/manual/en/mysqli-stmt.get-result.php

Advertisements

Published by:

Skyrail

Production Facilitator at Audacious Church Previously Software Developer at UKFast Ltd. and Web Applications Developer at SenseLogix Ltd. Graduate of Renewable Energy BSc from the University of Exeter. Experience with PHP, MySQL, Javascript, CSS and HTML.

Categories PHP, Web Development2 Comments

2 thoughts on “Get result set from a prepared statement using MySQLi in PHP”

  1. Awesome! This resolves problem of having to use bind_result and declare variable for each column when using SELECT * FROM db_table. Please note that this is supported on PHP5.3&+

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s