note 104819 added to mysqli-stmt.bind-param
| From: | berniev at bje dot com dot au | Date: | Sat, 09 Jul 2011 01:37:36 +0000 |
| Subject: | note 104819 added to mysqli-stmt.bind-param | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181368@lists.php.net to get a copy of this message | ||
jk's solution is neat but with dynamic input I tripped up on bind_param second parameter wants
a reference, and pass-by-reference is deprecated now.
This worked for me:
<?php
// $fieldsArray is array of columnName,value pairs
$vals = array();
$names = array();
foreach ($fieldsArray as $name => $val) {
$vals[] = &$fieldsArray[$name]; // <<=== note &
$names[] = "
{$name}=?";
}
$namesStr = implode(',', ($names));
$db = new mysqli("localhost","root","","tests");
$prep = $db->prepare("INSERT INTO table SET {$namesStr}");
$ref = new ReflectionClass('mysqli_stmt');
$meth = $ref->getMethod('bind_param');
$meth->invokeArgs($prep, $vals);
$prep->execute();
$insertedId = $prep->insert_id;
$prep->close();
?>
----
Server IP: 69.147.83.197
Probable Submitter: 121.210.26.87
----
Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-param.php
Edit -- https://master.php.net/note/edit/104819
Del: integrated -- https://master.php.net/note/delete/104819/integrated
Del: useless -- https://master.php.net/note/delete/104819/useless
Del: bad code -- https://master.php.net/note/delete/104819/bad+code
Del: spam -- https://master.php.net/note/delete/104819/spam
Del: non-english -- https://master.php.net/note/delete/104819/non-english
Del: in docs -- https://master.php.net/note/delete/104819/in+docs
Del: other reasons-- https://master.php.net/note/delete/104819
Reject -- https://master.php.net/note/reject/104819
Search -- https://master.php.net/manage/user-notes.php