note 85594 added to mysqli-stmt.bind-param

From: Date: Sun, 07 Sep 2008 21:26:09 +0000
Subject: note 85594 added to mysqli-stmt.bind-param
Groups: php.notes 
Request: Send a blank email to php-notes+get-144231@lists.php.net to get a copy of this message
I wanted to pass the parameters for several queries to a single function to fill them (insert / update having the same fields for example), while at the same time making the types array a bit easier to maintain when you've got a lot of parameters. Here's a simple solution I came up with: <?php function bindParameters(&$statement, &$params) { $args = array(); $args[] = implode('', array_values($params)); foreach ($params as $paramName => $paramType) { $args[] = &$params[$paramName]; $params[$paramName] = null; } call_user_func_array(array(&$statement, 'bind_param'), $args); } // Usage: $statement = $database->prepare('INSERT INTO test (value1, value2) VALUES (?, ?)'); $params = array('param1' => 's', 'param2' => 'i'); bindParameters($statement, $params); $params['param1'] = 'parameter test'; $params['param2'] = 42; $statement->execute(); ?> Note that the types will be overwritten after a call to bindParameters to provide a sensible default (otherwise it will be used as the parameter value when you execute the statement), so you need to reinitialize the types if you want to bind it to another statement. ---- Server IP: 69.147.83.197 Probable Submitter: 213.84.175.19 ---- Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-param.php Edit -- https://master.php.net/note/edit/85594 Del: integrated -- https://master.php.net/note/delete/85594/integrated Del: useless -- https://master.php.net/note/delete/85594/useless Del: bad code -- https://master.php.net/note/delete/85594/bad+code Del: spam -- https://master.php.net/note/delete/85594/spam Del: non-english -- https://master.php.net/note/delete/85594/non-english Del: in docs -- https://master.php.net/note/delete/85594/in+docs Del: other reasons-- https://master.php.net/note/delete/85594 Reject -- https://master.php.net/note/reject/85594 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144231) next »