note 113398 added to mysqli-stmt.get-result

From: Date: Sun, 06 Oct 2013 08:39:57 +0000
Subject: note 113398 added to mysqli-stmt.get-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-196478@lists.php.net to get a copy of this message
I went through a lot of trouble on a server where mysqlnd wasn't available, and had a lot of headaches. If you don't have mysqlnd installed/loaded whatever, you will get an undefined reference when trying to call "mysqli_stmt_get_result()". I wrote my own mysqli_stmt_get_result() and a mysqli_result_fetch_array() to go with it. <?php class iimysqli_result { public $stmt, $nCols; } function iimysqli_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 * mysqli_query(). * * $stmt = mysqli_prepare($conn, ""); * mysqli_bind_param($stmt, "types", ...); * * $param1 = 0; * $param2 = 'foo'; * $param3 = 'bar'; * mysqli_execute($stmt); * $result _mysqli_stmt_get_result($stmt); * [ $arr = _mysqli_result_fetch_array($result); * || $assoc = _mysqli_result_fetch_assoc($result); ] * mysqli_stmt_close($stmt); * mysqli_close($conn); * * At the source level, there is no difference between this and mysqlnd. **/ $metadata = mysqli_stmt_result_metadata($stmt); $ret = new iimysqli_result; if (!$ret) return NULL; $ret->nCols = mysqli_num_fields($metadata); $ret->stmt = $stmt; mysqli_free_result($metadata); return $ret; } function iimysqli_result_fetch_array(&$result) { $ret = array(); $code = "return mysqli_stmt_bind_result(\$result->stmt "; for ($i=0; $i<$result->nCols; $i++) { $ret[$i] = NULL; $code .= ", \$ret['" .$i ."']"; }; $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; } ?> Hope this helps someone. ---- Server IP: 69.147.83.199 Probable Submitter: 190.59.17.83 ---- Manual Page -- http://php.net/manual/en/mysqli-stmt.get-result.php Edit -- https://master.php.net/note/edit/113398 Del: integrated -- https://master.php.net/note/delete/113398/integrated Del: useless -- https://master.php.net/note/delete/113398/useless Del: bad code -- https://master.php.net/note/delete/113398/bad+code Del: spam -- https://master.php.net/note/delete/113398/spam Del: non-english -- https://master.php.net/note/delete/113398/non-english Del: in docs -- https://master.php.net/note/delete/113398/in+docs Del: other reasons-- https://master.php.net/note/delete/113398 Reject -- https://master.php.net/note/reject/113398 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#196478) next »