Skip to content

Instantly share code, notes, and snippets.

@Jmayhak
Created May 20, 2011 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jmayhak/982898 to your computer and use it in GitHub Desktop.
Save Jmayhak/982898 to your computer and use it in GitHub Desktop.
Get the selected rows from the mysqli_stmt as an array full of standard objects using mysqli
<?php
/**
* Returns all the selected rows as an array full of objects
* @param MySQLi_STMT $stmt
* @return array
*/
public function getResult($stmt)
{
if (is_a($stmt, 'MySQLi_STMT')) {
$result = array();
$metadata = $stmt->result_metadata();
$fields = $metadata->fetch_fields();
for (; ;)
{
$pointers = array();
$row = new \stdClass();
$pointers[] = $stmt;
foreach ($fields as $field)
{
$fieldname = $field->name;
$pointers[] = &$row->$fieldname;
}
call_user_func_array('mysqli_stmt_bind_result', $pointers);
if (!$stmt->fetch())
break;
$result[] = $row;
}
$metadata->free();
return $result;
}
return array();
}
/**
* Gets the first row from a select statement
* @param MySQLi_STMT $stmt
* @return stdClass
*/
public function getRow($stmt)
{
$result = $this->getResult($stmt);
if ($result && !empty($result)) {
return $result[0];
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment