note 85470 added to mysqli-stmt.bind-result

From: Date: Mon, 01 Sep 2008 14:07:39 +0000
Subject: note 85470 added to mysqli-stmt.bind-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-144053@lists.php.net to get a copy of this message
lot of people don't like how bind_result works with prepared statements! it requires you to pass long list of parameters which will be loaded with column value when the function being called. To solve this, i used call_user_func_array function and result_metadata functions. which make easy and automatically returns an array of all columns results stored in an array with column names. please don't forget to change setting variables with your own credentials: <?php $host = 'localhost'; $user = 'root'; $pass = '1234'; $data = 'test'; $mysqli = new mysqli($host, $user, $pass, $data); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } if ($stmt = $mysqli->prepare('SELECT * FROM user WHERE usr_name LIKE ?')) { $param_1 = '%'; $stmt->bind_param('s', $param_1); $stmt->execute(); $meta = $stmt->result_metadata(); while ($field = $meta->fetch_field()) { $arr_result[] = &$t[$field->name]; } call_user_func_array(array($stmt, 'bind_result'), $arr_result); while($stmt->fetch()) { print_r($t); } $stmt->close(); } $mysqli->close(); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 212.62.97.21 (proxied: 77.232.98.5) ---- Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-result.php Edit -- https://master.php.net/note/edit/85470 Del: integrated -- https://master.php.net/note/delete/85470/integrated Del: useless -- https://master.php.net/note/delete/85470/useless Del: bad code -- https://master.php.net/note/delete/85470/bad+code Del: spam -- https://master.php.net/note/delete/85470/spam Del: non-english -- https://master.php.net/note/delete/85470/non-english Del: in docs -- https://master.php.net/note/delete/85470/in+docs Del: other reasons-- https://master.php.net/note/delete/85470 Reject -- https://master.php.net/note/reject/85470 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144053) next »