This is the error:
Warning: mysqli_stmt_bind_param(): Number of elements in type definition string doesn’t match number of bind variables
Let me show you a snippet of code followed by the output.
if (!empty($par_val)) { echo "$sql_update<br>"; // append par_type with 2 integers for $req_setstatus and $req_activator. $par_type .= 'ii'; echo "$par_type<br>"; // append par_val with $req_setstatus and $req_activator $par_val .= "$req_setstatus,$req_activator"; echo "$par_val"; mysqli_stmt_bind_param($stmt,$par_type,$par_val); }
here are the outputs:
UPDATE request SET priority = ?,status = ?, activator = ? WHERE id = 35 –> this is the update statement, 3 fields to update.
sii –>this is the $par_type, there are 3 values.
Urgent,2,6 –>this is the $par_val, there are 3 values.
Why am I receiving this error.
I’m thinking that possibly, it is the multiple values being tied into 1 single variable, but if this is the problem, then how would I set this up to take a dynamic set of variables each time it is ran. Sometimes the in inputs can be 3, sometimes 8.
Advertisement
Answer
If you need a dynamic amount of parameters, you can use $par_val
as an array. So…
$par_val = [2,6];
and then use the array unpacking operator (...
)…
mysqli_stmt_bind_param($stmt, $par_type, ...$par_val);
You could have $par_val
as a csv and use explode()
to make it into an array and then unpack it as above…
$par_val = explode(",", "2,6);