Mysqli Get_Result Alternative

Mysqli get_result alternative

Here is a neater solution based on the same principle as lx answer:

function get_result( $Statement ) {
$RESULT = array();
$Statement->store_result();
for ( $i = 0; $i < $Statement->num_rows; $i++ ) {
$Metadata = $Statement->result_metadata();
$PARAMS = array();
while ( $Field = $Metadata->fetch_field() ) {
$PARAMS[] = &$RESULT[ $i ][ $Field->name ];
}
call_user_func_array( array( $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.

MySQLi get_result alternative for PHP 5.3?

You can try using fetch() with bind_result() instead.

public function get_setting($setting) {
// Prepate statement
$prepared = $this->prepare("SELECT `val` FROM `filex_settings` WHERE `setting`=?", 'get_setting');
$this->bind_param($prepared->bind_param('s', $setting), 'get_setting()');
$this->execute($prepared, 'get_setting()');
$this->bind_result($col_1,$col_2,..)
while($this->fetch())
{
return $col_n // the value which you want
}
}

mysqli_stmt_get_result alternative for php 5.2.6

The mysqli_stmt_get_result function is PHP 5.3 or greater only. It does not exist for your PHP 5.2.x version (which is not supported any longer btw.).

The alternative is to use mysqli_stmt_bind_result with variable bindings.

In your concrete example this has even the benefit that you do not need to assign the array members to variables, because you can bind the variables directly.

The mysqli_stmt_get_result function was introduced because someone thought this would stand in your way and getting an array would have been easier.



Related Topics



Leave a reply



Submit