Mysqli get_result alternative
mysqli, php
Solution
Here is a neater solution based on the same principle as lx answer:
function get_result(mysqli_stmt $Statement): array
{
$RESULT = array();
$Statement->store_result();
$Metadata = $Statement->result_metadata();
for ($i = 0; $i < $Statement->num_rows; $i++) {
$PARAMS = array();
while ($Field = $Metadata->fetch_field()) {
$PARAMS[] = &$RESULT[$i][$Field->name];
}
$Statement->bind_result(...$PARAMS);
$Statement->fetch();
}
return $RESULT;
}
With mysqlnd you would normally do:
$Statement = $Database->prepare( 'SELECT x FROM y WHERE z = ?' );
$Statement->bind_param( 's', $z );
$Statement->execute();
$Result = $Statement->get_result();
while ( $DATA = $Result->fetch_array() ) {
// Do stuff with the data
}
And without mysqlnd:
$Statement = $Database->prepare( 'SELECT x FROM y WHERE z = ?' );
$Statement->bind_param( 's', $z );
$Statement->execute();
$RESULT = get_result( $Statement );
while ( $DATA = array_shift( $RESULT ) ) {
// Do stuff with the data
}
So the usage and syntax are almost identical. The main difference is that the replacement function returns a result array, rather than a result object.
Problem
I've just changed all my sql queries to prepared statements using mysqli. To speed this process up I created a function (called `performQuery`) which replaces `mysql_query`. It takes the query, the bindings (like "sdss") and the variables to pass in, this then does all the perpared statement stuff. This meant changing all my old code was easy. My function returns a `mysqli_result` object using mysqli `get_result()`. This meant I could change my old code from: ``` $query = "SELECT x FROM y WHERE z = $var"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)){ echo $row['x']; } ``` to ``` $query = "SELECT x FROM y WHERE z = ?"; $result = performQuery($query,"s",$var); while ($row = mysql_fetch_assoc($result)){ echo $row['x']; } ``` This works fine on localhost, but my web hosting server does not have mysqlnd available, therefore `get_result()` does not work. Installing mysqlnd is not an option. What is the best way to go from here? Can I create a function which replaces `get_result()`, and how?