bind_param PHP 中的错误:"number of variables doesnt match number of parameters"
bind_param error in PHP: "number of variables doesnt match number of parameters"
我很清楚这个问题已经被问过了,我真的很抱歉再次问,但是其他人没有回答我的情况。我不知道这个 bind_param 有什么问题。这是我的 PHP:
的代码
<?php
$db = new mysqli("localhost", "HIDDEN", "HIDDEN", "HIDDEN");
if ($db->connect_error) {
die("Sorry, there was a problem connecting to our database.");
}
$username = stripslashes(htmlspecialchars($_GET['username']));
$result = $db->prepare("SELECT * FROM messages");
$result->bind_param("s", $username);
$result->execute();
$result = $result->get_result();
while ($r = $result->fetch_row()) {
echo $r[1];
echo "\";
echo $r[2];
echo "\n";
}
这个 bind_param 有什么问题吗?抱歉再次打扰,感谢您的帮助。
那是因为您试图在此处 $result->bind_param("s", $username);
绑定变量 s
。
问题是您没有在查询中使用它。
你应该在prepare语句中使用placeholders
(问号,?
),并且bind_param
方法的参数个数应该是[=11=的1+num] ] 符号。
第一个参数表示参数的数据类型,然后是绑定参数值。
在您的示例中,您将 2 个参数传递给 bind_param 方法(第一个参数,s
代表字符串,第二个参数,$username
代表值。),但是有您的查询中没有占位符 ?
。
我很清楚这个问题已经被问过了,我真的很抱歉再次问,但是其他人没有回答我的情况。我不知道这个 bind_param 有什么问题。这是我的 PHP:
的代码<?php
$db = new mysqli("localhost", "HIDDEN", "HIDDEN", "HIDDEN");
if ($db->connect_error) {
die("Sorry, there was a problem connecting to our database.");
}
$username = stripslashes(htmlspecialchars($_GET['username']));
$result = $db->prepare("SELECT * FROM messages");
$result->bind_param("s", $username);
$result->execute();
$result = $result->get_result();
while ($r = $result->fetch_row()) {
echo $r[1];
echo "\";
echo $r[2];
echo "\n";
}
这个 bind_param 有什么问题吗?抱歉再次打扰,感谢您的帮助。
那是因为您试图在此处 $result->bind_param("s", $username);
绑定变量 s
。
问题是您没有在查询中使用它。
你应该在prepare语句中使用placeholders
(问号,?
),并且bind_param
方法的参数个数应该是[=11=的1+num] ] 符号。
第一个参数表示参数的数据类型,然后是绑定参数值。
在您的示例中,您将 2 个参数传递给 bind_param 方法(第一个参数,s
代表字符串,第二个参数,$username
代表值。),但是有您的查询中没有占位符 ?
。