Skip to content

Instantly share code, notes, and snippets.

@silvioq
Last active August 29, 2015 14:22
Show Gist options
  • Save silvioq/5fe5dd8b2e070152a13e to your computer and use it in GitHub Desktop.
Save silvioq/5fe5dd8b2e070152a13e to your computer and use it in GitHub Desktop.
class iimysqli_result
{
public $stmt, $nCols, $field;
public function fetch_array()
{
$ret = array();
$code = "return mysqli_stmt_bind_result(\$this->stmt ";
for ($i=0; $i<$this->nCols; $i++)
{
$ret[$i] = NULL;
$code .= ", \$ret['" .$i ."']";
};
$code .= ");";
if (!eval($code)) { return NULL; };
// This should advance the "$stmt" cursor.
if (!mysqli_stmt_fetch($this->stmt)) { return NULL; };
// Return the array we built.
return $ret;
}
public function fetch_assoc()
{
$data = $this->fetch_array();
if( !$data ) return null;
$ret = array();
foreach( $this->fields as $field )
{
$ret[$field] = array_shift( $data );
}
return $ret;
}
}
# Idea tomada de http://php.net/manual/es/mysqli-stmt.get-result.php#113398
function mysqli_stmt_get_result($stmt)
{
/** EXPLANATION:
* We are creating a fake "result" structure to enable us to have
* source-level equivalent syntax to a query executed via
**/
$metadata = mysqli_stmt_result_metadata($stmt);
$ret = new iimysqli_result;
if (!$ret) return NULL;
$ret->nCols = mysqli_num_fields($metadata);
$ret->fields = array();
while( $field = $metadata->fetch_field() )
{
$ret->fields[] = $field->name;
}
$stmt->execute();
$stmt->store_result();
$ret->stmt = $stmt;
$ret->num_rows = $stmt->num_rows;
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment