Created
May 20, 2011 13:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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