[英]Why am I receiving a bind parameter issue?
This is the error:这是错误:
Warning: mysqli_stmt_bind_param(): Number of elements in type definition string doesn't match number of bind variables
警告:mysqli_stmt_bind_param():类型定义字符串中的元素数与绑定变量数不匹配
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 = ?
更新请求 SET 优先级 = ?,状态 = ?, 激活器 = ? WHERE id = 35 --> this is the update statement, 3 fields to update.
WHERE id = 35 --> 这是更新语句,要更新的 3 个字段。
sii -->this is the $par_type, there are 3 values.
sii --> 这是 $par_type,有 3 个值。
Urgent,2,6 -->this is the $par_val, there are 3 values.
Urgent,2,6 -->这是$par_val,有3个值。
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.我在想,这可能是将多个值绑定到 1 个单个变量中,但如果这是问题所在,那么我将如何设置它以在每次运行时采用一组动态变量。 Sometimes the in inputs can be 3, sometimes 8.
有时输入可以是 3,有时是 8。
If you need a dynamic amount of parameters, you can use $par_val
as an array.如果您需要动态数量的参数,您可以使用
$par_val
作为数组。 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
作为 csv 并使用$par_val
explode()
将其放入数组中,然后将其解压缩如上...
$par_val = explode(",", "2,6);
I would recommend to use OOP-style, which is easier to read and also use string type ( s
) when you are binding unknown number of parameters.我建议使用 OOP 样式,它更易于阅读,并且在绑定未知数量的参数时也使用字符串类型 (
s
)。 You really don't need to distinguish the integers, and when dynamically binding it makes no difference most of the time whether you bind as string or as integer.您真的不需要区分整数,并且在动态绑定时,大多数情况下您绑定为字符串还是整数都没有区别。
First build and array with your parameters.首先使用您的参数构建和排列。
$params = [$req_setstatus, $req_activator, $whateverIsYour3rdParam];
Then repeat type s
the correct number of times.然后重复 type
s
正确的次数。
$types = str_repeat('s', count($params));
Then bind it.然后绑定。 You can use the splat operator (argument unpacking)
...
before the array to unpack the array into its constituents.您可以在数组之前使用 splat 运算符(参数解包)
...
将数组解包为其组成部分。
$stmt->bind_param($types, ...$params);
As an ultimate recommendation, I would advice to use PDO, which is much easier to use.作为最终建议,我建议使用 PDO,它更易于使用。 Even better start using a good abstraction library, eg EasyDB.
最好开始使用一个好的抽象库,例如 EasyDB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.