note 71056 added to function.mysqli-stmt-fetch

From: Date: Thu, 09 Nov 2006 21:33:52 +0000
Subject: note 71056 added to function.mysqli-stmt-fetch
Groups: php.notes 
Request: Send a blank email to php-notes+get-119571@lists.php.net to get a copy of this message
Having just learned about call_user_func_array I reworked my fetch_assoc example. Swapping the following code makes for a more elegant (and faster) solution. Using: <?php while ($columnName = $meta->fetch_field()) { $columns[] = &$results[$columnName->name]; } call_user_func_array(array($stmt, 'bind_result'), $columns); ?> Instead of this code from my example below: <?php $bindResult = '$stmt->bind_result('; while ($columnName = $meta->fetch_field()) { $bindResult .= '$results["'.$columnName->name.'"],'; } $bindResult = rtrim($bindResult, ',') . ');'; eval($bindResult); ?> The full reworked fetch_assoc code for reference: <?php $mysqli = new mysqli($dbHost, $dbUsername, $dbPassword, $dbDatabase); $stmt = $mysqli->prepare('select * from foobar'); $stmt->execute(); $stmt->store_result(); $meta = $stmt->result_metadata(); while ($column = $meta->fetch_field()) { $bindVarsArray[] = &$results[$column->name]; } call_user_func_array(array($stmt, 'bind_result'), $bindVarsArray); $stmt->fetch(); echo var_dump($results); // outputs: // // array(3) { // ["id"]=> // &int(1) // ["foo"]=> // &string(11) "This is Foo" // ["bar"]=> // &string(11) "This is Bar" // } ?> ---- Server IP: 83.138.144.80 Probable Submitter: 62.252.224.12 (proxied: 82.3.207.56) ---- Manual Page -- http://www.php.net/manual/en/function.mysqli-stmt-fetch.php Edit -- https://master.php.net/note/edit/71056 Del: integrated -- https://master.php.net/note/delete/71056/integrated Del: useless -- https://master.php.net/note/delete/71056/useless Del: bad code -- https://master.php.net/note/delete/71056/bad+code Del: spam -- https://master.php.net/note/delete/71056/spam Del: non-english -- https://master.php.net/note/delete/71056/non-english Del: in docs -- https://master.php.net/note/delete/71056/in+docs Del: other reasons-- https://master.php.net/note/delete/71056 Reject -- https://master.php.net/note/reject/71056 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119571) next »