note 104819 deleted from mysqli-stmt.bind-param by danbrown
| From: | danbrown@php.net | Date: | Sat, 09 Jul 2011 16:24:04 +0000 |
| Subject: | note 104819 deleted from mysqli-stmt.bind-param by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181382@lists.php.net to get a copy of this message | ||
Note Submitter: berniev at bje dot com dot au
----
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();
?>