3

When I try to below code it give me a warning

mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables

$stmt = $mysqli->prepare('SELECT * FROM users WHERE lname = ? AND fname = ?');
    $type = "ss";
    $param = array("Maq","bool");
    $params[] = &$type;
    $params[] = &$param;

    call_user_func_array( array($stmt, 'bind_param'), $params );

I really don't know why its happening I read a lot of same title questions in Stack Overflow but some has comma separated types or need more type but in my case I think its proper but why I am keep getting warnings?

2 Answers 2

5

This is a late answer, I hope will help someone.

It's a part of my mysqliLayer class, so I extracted needed variables.

// establish mysqli connection
$conn = new mysqli(.....); 
$tableName = 'users';
// Types to bind
$type = 'isss';
$fields = ['id','name', 'email', 'created'];
$values = [1, 'angel', '[email protected]', '2018-1-12'];

$sql = "INSERT INTO " . $tableName . " (" . join(',', $fields) . ") VALUES (?,?,?,?)";

$stmt = $conn->prepare($sql);
// Using ...token introduced in php v.5.6 instead of call_user_func_array
// This way references can be omitted, like for each value in array
$stmt->bind_param($type, ...$values);

$stmt->execute();

$stmt->close();
Sign up to request clarification or add additional context in comments.

Comments

4
$stmt = $mysqli->prepare('SELECT * FROM users WHERE lname = ? AND fname = ?');
$stmt->bind_param('ss', $param[0], $param[1]);
$stmt->execute();

//other version
$stmt = $mysqli->prepare('SELECT * FROM users WHERE lname = ? AND fname = ?');
$type = "ss";
$names = array("Maq", "bool");
$params = array(&$type, &$names[0], &$names[1]);

call_user_func_array(array($stmt, 'bind_param'), $params);

6 Comments

i want it dynamically please :)
Like this ? About the call_user_func_array, please see : no2.php.net/manual/en/mysqli-stmt.bind-param.php#89171
thanks @Spoke44 but he is using procedural style i have to stuck with OOP :/
thankkkkk uu so much its working but what was the problem i did not get it??
The array params must contain only values. You passed to it an array of values !
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.