Bind Param with array of parameters
call_user_func_array
"Call a callback with an array of parameters"
call_user_func_array(array($stmt, "bind_param"), array_merge(array($type), $params));
should do the job
UPDATE: you have also to change your params array:
$params = array(&$firstName, &$lastName, &$address, &$postcode, &$email, &$password);
as mysqli_stmt::bind_param
expects the second and the following parameters by reference.
EDIT: Your query seems to be wrong. Maybe you have less fields than you have variables there. Do:
"INSERT INTO Users (field1, field2, field3, field4, field5, field6) VALUES (?, ?, ?, ?, ?, ?)"
where you replace the name of the fields by the correct names
How to pass a list of parameters contained in an array to bind_param?
Using PHP 5.6, you can do this easily with the help of the unpacking Operator (...$var
) and use get_result() instead of bind_result().
$stmt->bind_param($types, ...$list);
$stmt->get_result();
Sending an array of parameters to bind_param
try like this
$mysqli = new mysqli('localhost', 'root','','mydb');
$stmt=$mysqli->prepare("select * from blog where id=? and date=?");
$title='1';
$text='2016-04-07';
call_user_func_array(array($stmt, "bind_param"),array_merge(array('ss'),array(&$title,&$text)));
$stmt->execute();
$result = $stmt->get_result();
print_r($result->fetch_array());
echo $stmt->error;
ok if you have parameters coming from the array
$mysqli = new mysqli('localhost', 'root','','jobspace');
$stmt=$mysqli->prepare("select * from listings where listing_type_sid=? and user_sid=?");
$title='6';
$text='8';
$arr=array($title,$text);
foreach($arr as &$ar){
$new[]=&$ar;
}
$types = implode(array_fill(0,count($arr),'s'));
call_user_func_array(array($stmt, "bind_param"),array_merge(array($types),$new));
$stmt->execute();
$result = $stmt->get_result();
print_r($result->fetch_array());
echo $stmt->error;
PHP bind_param with arrays
If your PHP is not outdated (>= 5.6 namely), just add three dots to the first example,
$stmt->bind_param('iss', ...array(101, 'SomeString 1', 'Some string 2'));
php pdo bind array parameters in bindParam without foreach loop
Many PDO users think they have to use bindParam()
. You don't.
You can pass an array directly to execute()
with all your parameter values. It's this easy:
$stmt->execute($data);
If you used named parameters in your SQL, use a hash array. If you used positional parameters, use a plain array.
For more complete code examples, read them here: http://php.net/manual/en/pdo.prepare.php
MySQLi Bind Param with an array for IN
This is a scenario where doing it this way is inappropriate. You're constructing actual SQL (that's what the commas and quotes are), and passing it in as a parameter. It's basically evaluating to value3 IN ('...')
where ...
is the entirety of $values
.
Also that's a good call about the quotes. MySQL uses single quotes.
You'll need to either build the SQL using string concatenation alone, or use more than one parameter.
EDIT
As an example:
$values = array('a','b','c','d');
$values = "'" . implode("','", $values) . "'";
$stmt->prepare('SELECT value1, value2 FROM table1 WHERE value3 IN (' . $values . ')');
Related Topics
PHP Password_Verify() Hash and Pass Won't Match
How to Escape Single-Quote (Apostrophe) in String Using PHP
How to Create a Twig Custom Tag That Executes a Callback
Laravel Mail: Pass String Instead of View
Non-Static Method ..... Should Not Be Called Statically
How to Query All the Graphql Type Fields Without Writing a Long Query
Reconnection of Client When Server Reboots in Websocket
How to Password-Protect PHP Page
How to Set Up Use Httponly Cookies in PHP
Running a Zend Framework Action from Command Line
Avoid Public Folder of Laravel and Open Directly the Root in Web Server
How to Use Break or Continue Within for Loop in Twig Template
How to Alias a Function in PHP
How to Display Xml in HTML in PHP
Mysql_Escape_String VS MySQL_Real_Escape_String