note 70673 added to function.mysqli-stmt-fetch

From: Date: Mon, 23 Oct 2006 20:07:08 +0000
Subject: note 70673 added to function.mysqli-stmt-fetch
Groups: php.notes 
Request: Send a blank email to php-notes+get-119030@lists.php.net to get a copy of this message
I wanted a simple way to get the equivalent of fetch_assoc when using a prepared statement. I came up with the following: <?php $mysqli = new mysqli($dbHost, $dbUsername, $dbPassword, $dbDatabase); $stmt = $mysqli->prepare('select * from foobar'); $stmt->execute(); $stmt->store_result(); $meta = $stmt->result_metadata(); // the following creates a bind_result string with an argument for each column in the query // e.g. $stmt->bind_result($results["id"], $results["foo"], $results["bar"]); $bindResult = '$stmt->bind_result('; while ($columnName = $meta->fetch_field()) { $bindResult .= '$results["'.$columnName->name.'"],'; } $bindResult = rtrim($bindResult, ',') . ');'; // executes the bind_result string eval($bindResult); $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: 85.116.0.130 Probable Submitter: 62.252.224.13 (proxied: 82.3.206.112) ---- Manual Page -- http://www.php.net/manual/en/function.mysqli-stmt-fetch.php Edit -- https://master.php.net/note/edit/70673 Del: integrated -- https://master.php.net/note/delete/70673/integrated Del: useless -- https://master.php.net/note/delete/70673/useless Del: bad code -- https://master.php.net/note/delete/70673/bad+code Del: spam -- https://master.php.net/note/delete/70673/spam Del: non-english -- https://master.php.net/note/delete/70673/non-english Del: in docs -- https://master.php.net/note/delete/70673/in+docs Del: other reasons-- https://master.php.net/note/delete/70673 Reject -- https://master.php.net/note/reject/70673 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119030) next »