note 118300 added to mysqli-stmt.get-result
| From: | tonywoodecode at gmail dot com | Date: | Wed, 11 Nov 2015 12:09:38 +0000 |
| Subject: | note 118300 added to mysqli-stmt.get-result | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204137@lists.php.net to get a copy of this message | ||
well I for one found anonymous' above eval-trick code very useful, seeing as no one has posted
a working alternative! However, because I wanted to cater for a generic database object, I found it
a problem that the code assumed numeric array was fine for all callsites, and I needed to cater for
all callers using assoc arrays exlusively. I came up with this:
<?php
class IImysqli_result {
public $stmt, $ncols;
}
class DBObject {
function iimysqli_get_result($stmt) {
$metadata = $stmt->result_metadata();
$ret = new IImysqli_result;
if (!$ret || !$metadata) return NULL; //the latter because this gets called whether we are
adding/updating as well as returning
$ret->ncols = $metadata->field_count;
$ret->stmt = $stmt;
$metadata->free_result();
return $ret;
}
//this mimics mysqli_fetch_array by returning a new row each time until exhausted
function iimysqli_result_fetch_array(&$result) {
$stmt = $result->stmt;
$stmt->store_result();
$resultkeys = array();
$thisName = "";
for ( $i = 0; $i < $stmt->num_rows; $i++ ) {
$metadata = $stmt->result_metadata();
while ( $field = $metadata->fetch_field() ) {
$thisName = $field->name;
$resultkeys[] = $thisName;
}
}
$ret = array();
$code = "return mysqli_stmt_bind_result(\$result->stmt ";
for ($i=0; $i<$result->ncols; $i++) {
$ret[$i] = NULL;
$theValue = $resultkeys[$i];
$code .= ", \$ret['$theValue']";
}
$code .= ");";
if (!eval($code)) {
return NULL;
}
// This should advance the "$stmt" cursor.
if (!mysqli_stmt_fetch($result->stmt)) {
return NULL;
}
// Return the array we built.
return $ret;
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 31.221.59.249
----
Manual Page -- http://php.net/manual/en/mysqli-stmt.get-result.php
Edit -- https://master.php.net/note/edit/118300
Del: integrated -- https://master.php.net/note/delete/118300/integrated
Del: useless -- https://master.php.net/note/delete/118300/useless
Del: bad code -- https://master.php.net/note/delete/118300/bad+code
Del: spam -- https://master.php.net/note/delete/118300/spam
Del: non-english -- https://master.php.net/note/delete/118300/non-english
Del: in docs -- https://master.php.net/note/delete/118300/in+docs
Del: other reasons-- https://master.php.net/note/delete/118300
Reject -- https://master.php.net/note/reject/118300
Search -- https://master.php.net/manage/user-notes.php