Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQLI binding params using call_user_func_array

Tags:

php

bind

mysqli

Please see below my code. I am attempting to bind an array of paramenters to my prepared statement. I've been looking around on the web and can see I have to use call_user_func_array but cannot get it to work. The error I get is: "First argument is expected to be a valid callback, 'Array' was given" I may be wrong but I'm assuming the first argument can be an an array and perhaps this error message is misleading. I think the issue is that my array is in someway at fault. Can anyone see what I am doing wrong? Thanks.

$type = array("s", "s");
$param = array("string1","anotherstring");

$stmt = $SQLConnection->prepare("INSERT INTO mytable (comp, addl) VALUES (?,?)");

$params = array_merge($type, $param);

call_user_func_array(array(&$stmt, 'bind_param'), $params);
$SQLConnection->execute();
like image 386
Columbo Avatar asked Dec 16 '09 10:12

Columbo


People also ask

How to use bind_ param in PHP?

where we want to substitute in an integer, string, double or blob value. Then, have a look at the bind_param() function: $stmt->bind_param("sss", $firstname, $lastname, $email); This function binds the parameters to the SQL query and tells the database what the parameters are.

What is the use of Mysqli_stmt_bind_param?

The mysqli_stmt_bind_param() function is used to bind variables to the parameter markers of a prepared statement.

What is Mysqli_prepare?

Definition and Usage The prepare() / mysqli_prepare() function is used to prepare an SQL statement for execution.

What is prepared statement in php?

The prepared statement is a special function embedded in PHP which allows programmers to write codes that can be executed multiple times in an efficient manner by our database. When communicating with your database, extra care is given to security, query time, and server request size.


2 Answers

It must be like this:

//connect
$mysqli = new mysqli($host, $user, $password, $db_name);

//prepare
$stmt = $mysqli->prepare("SELECT * FROM the_table WHERE field1= ? AND Field2= ?");

//Binding parameters. Types: s = string, i = integer, d = double,  b = blob
$params= array("ss","string_1","string_2");

//now we need to add references
$tmp = array();
foreach($params as $key => $value) $tmp[$key] = &$params[$key];
// now us the new array
call_user_func_array(array($stmt, 'bind_param'), $tmp);

$stmt->execute();

/* Fetch result to array */
$res = $stmt->get_result();
while($row = $res->fetch_array(MYSQLI_ASSOC)) {
  $a_data[]=$row;
}
print_r($a_data);

$stmt->close();
like image 89
abd.agha Avatar answered Oct 13 '22 01:10

abd.agha


Since PHP 5.6, you don't have to mess around with call_user_func_array() anymore.

Instead of:

$stmt->bind_param($param_types, $my_params_array);

you can just use the splat operator, like this:

$stmt->bind_param($param_types, ...$my_params_array); // exact code

like image 35
ban-geoengineering Avatar answered Oct 12 '22 23:10

ban-geoengineering