我有以下代码:
$conn = new mysqli($DB, $username, $password, $dbname);
if($condition == 'con1'){
$sql = "UPDATE users SET con1=? WHERE name=?";
}elseif($condition == 'con2'){
$sql = "UPDATE users SET con2=? WHERE name=?";
}elseif($condition == 'con3'){
$sql = "UPDATE users SET con3=? WHERE name=?";
}
if($query = $conn->prepare($sql)) {
$query->bind_param($value,$name);
$query->execute();
print_r($query);
} else {
$error = $conn->errno . ' ' . $conn->error;
echo $error;
}
然后我得到了那个错误:
Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables on line 32
从$query
mysqli_stmt Object
(
[affected_rows] => -1
[insert_id] => 0
[num_rows] => 0
[param_count] => 2
[field_count] => 0
[errno] => 2031
[error] => No data supplied for parameters in prepared statement
[error_list] => Array
(
[0] => Array
(
[errno] => 2031
[sqlstate] => HY000
[error] => No data supplied for parameters in prepared statement
)
)
[sqlstate] => HY000
[id] => 1
)
有2个?和2个变量,我得到的错误是说参数的数量不等于变量的数量。
有什么问题?
答案 0 :(得分:2)
在您的案例问题中,您是否使用了 - > bind_param函数错误。
// prepare and bind
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email); // sss means string string string
// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt->execute();
论证可能是以下四种类型之一:
我认为正确的代码应如下所示:
$conn = new mysqli($DB, $username, $password, $dbname);
// You can avoid this structure, by doing concatenation!!!**
if($condition == 'con1'){
$sql = "UPDATE users SET con1=? WHERE name=?";
}elseif($condition == 'con2'){
$sql = "UPDATE users SET con2=? WHERE name=?";
}elseif($condition == 'con3'){
$sql = "UPDATE users SET con3=? WHERE name=?";
}
if($query = $conn->prepare($sql)) {
$query->bind_param("ss", $con, $name);
$con = 'yourNeededValue';
$name = 'someName?';
$query->execute();
} else {
$error = $conn->errno . ' ' . $conn->error;
echo $error;
}