note 111653 added to mysqli-stmt.bind-param
| From: | mikechip | Date: | Wed, 13 Mar 2013 02:45:40 +0000 |
| Subject: | note 111653 added to mysqli-stmt.bind-param | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194050@lists.php.net to get a copy of this message | ||
I had to do a more advanced prepare function (A multi-multi values prepared statement)to make things
easier for a few situations, so I wrote the following function.
NOTE: This function assumes you have put it into a class that have:
$mysqli = A mysqli object
NOTE2: This function currently only supports ints for the values, but you can modify it to support
additional types...
To use it:
<?php
$sql = "SELECT id, code FROM country";
$result = $database->fetch_all($sql); // This is just a custom fetch_all function...
if(!$result) {
return null; // no results...
}
// Re-organize the order of the elements from the resultset, code first then id, just like the sql
statement...
$pData = array();
foreach($result as $row) {
$pData[] = array('code'=>$row['code'] +
1,'id'=>$row['id']); //Assign valid php variable names, because this'll
be used later on!!!
}
$database->mysqli_prepare_exec("UPDATE country SET code=? WHERE id=?", 'ii',
$pData);
?>
Here's the function:
<?php
public function mysqli_prepare_exec($sql, $types, $data)
{
$stm = $this->mysqli->prepare($sql);
if(!$stm) {
return null; // Statement couldn't be prepared, check for errors...
}
foreach($data[0] as $k => $v) {
eval('$' . $k . '=' . $v . ';');
eval('$vars[\'' . $k . '\'] = &$' . $k . ';'); //
Referencing to our last newly created variable
}
unset($k,$v);
call_user_func_array(array($stm, 'bind_param'), array_merge(array($types), $vars));
foreach($data as $row) {
foreach($row as $k => $v) {
eval('$' . $k . '=' . $v . ';');
}
$stm->execute();
}
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 177.106.157.42
----
Manual Page -- http://php.net/manual/en/mysqli-stmt.bind-param.php
Edit -- https://master.php.net/note/edit/111653
Del: integrated -- https://master.php.net/note/delete/111653/integrated
Del: useless -- https://master.php.net/note/delete/111653/useless
Del: bad code -- https://master.php.net/note/delete/111653/bad+code
Del: spam -- https://master.php.net/note/delete/111653/spam
Del: non-english -- https://master.php.net/note/delete/111653/non-english
Del: in docs -- https://master.php.net/note/delete/111653/in+docs
Del: other reasons-- https://master.php.net/note/delete/111653
Reject -- https://master.php.net/note/reject/111653
Search -- https://master.php.net/manage/user-notes.php