note 89098 added to mysqli-stmt.bind-result

From: Date: Sat, 21 Feb 2009 18:41:19 +0000
Subject: note 89098 added to mysqli-stmt.bind-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-150923@lists.php.net to get a copy of this message
/////////////////////////////// Im sure many of you may use this, i spent about 3hours writing this, so maybe i can save somone else some time. the mysqli bind param function only takes one variable at a time, so its difficult to pass in a few variables to fill in the placeholder space. this allows mysqli prepared statements with variable arguments, one sql template with multiple placeholders to be prepared and excuted. returns array of arrays, where the key names are the column names mysqli_stmt_bind_result hope this helps somone, Mahees. /////////////////////////////// $uname = 'mahees'; $pass = 'mahees'; $userPassArr = DataAccess::fetch('SELECT * FROM users WHERE username = ? AND password = ?', $uname, $pass); print_r($userPassArr); /* Array ( [0] => Array ( [id] => 1 [username] => mahees [password] => mahees ) ) */ $userPassArr = DataAccess::fetch('SELECT * FROM users'); print_r($userPassArr); /* Array ( [0] => Array ( [id] => 1 [username] => mahees [password] => mahees ) [1] => Array ( [id] => 4 [username] => foo [password] => bar ) [2] => Array ( [id] => 5 [username] => bar [password] => baz ) ) */ //********* function in DataAccess class ********* //im sure this can be witten better with more checks...but principle stands static function fetch() { $args = func_get_args(); $sql = array_shift($args); $link = self::establish_db_conn(); if (!$stmt = mysqli_prepare($link, $sql)) { self::close_db_conn(); die('Please check your sql statement : unable to prepare'); } $types = str_repeat('s', count($args)); array_unshift($args, $types); array_unshift($args, $stmt); call_user_func_array('mysqli_stmt_bind_param', $args); mysqli_stmt_execute($stmt); $result = mysqli_stmt_result_metadata($stmt); $fields = array(); while ($field = mysqli_fetch_field($result)) { $name = $field->name; $fields[$name] = &$$name; } array_unshift($fields, $stmt); call_user_func_array('mysqli_stmt_bind_result', $fields); array_shift($fields); $results = array(); while (mysqli_stmt_fetch($stmt)) { $temp = array(); foreach($fields as $key => $val) { $temp[$key] = $val; } array_push($results, $temp); } mysqli_free_result($result); mysqli_stmt_close($stmt); self::close_db_conn(); return $results; } ---- Server IP: 69.147.83.197 Probable Submitter: 72.252.106.15 ---- Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-result.php Edit -- https://master.php.net/note/edit/89098 Del: integrated -- https://master.php.net/note/delete/89098/integrated Del: useless -- https://master.php.net/note/delete/89098/useless Del: bad code -- https://master.php.net/note/delete/89098/bad+code Del: spam -- https://master.php.net/note/delete/89098/spam Del: non-english -- https://master.php.net/note/delete/89098/non-english Del: in docs -- https://master.php.net/note/delete/89098/in+docs Del: other reasons-- https://master.php.net/note/delete/89098 Reject -- https://master.php.net/note/reject/89098 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150923) next »