I wanted a simple way to get the equivalent of fetch_assoc when using a prepared statement. I came up with the following:
<?php
$mysqli = new mysqli($dbHost, $dbUsername, $dbPassword, $dbDatabase);
$stmt = $mysqli->prepare('select * from foobar');
$stmt->execute();
$stmt->store_result();
$meta = $stmt->result_metadata();
// the following creates a bind_result string with an argument for each column in the query
// e.g. $stmt->bind_result($results["id"], $results["foo"], $results["bar"]);
$bindResult = '$stmt->bind_result(';
while ($columnName = $meta->fetch_field()) {
$bindResult .= '$results["'.$columnName->name.'"],';
}
$bindResult = rtrim($bindResult, ',') . ');';
// executes the bind_result string
eval($bindResult);
$stmt->fetch();
echo var_dump($results);
// outputs:
//
// array(3) {
// ["id"]=>
// &int(1)
// ["foo"]=>
// &string(11) "This is Foo"
// ["bar"]=>
// &string(11) "This is Bar"
// }
?>