我正在尝试使用pdo上的prepare语句插入值,但我正面临此错误
警告:PDOStatement :: execute()[pdostatement.execute]: SQLSTATE [HY093]:参数号无效:没有绑定参数 第23行的C:\ wamp \ www \ chat \ insert.php
$db = new PDO($dns, $user, $pass);
$sql = "INSERT INTO `conversations_t` (`fk_crname_c`, `fk_username_c`, `message_c`) VALUES (?, ?, ?)";
$sq_stat = $db->prepare($sql);
// bind column to variable for display
$sq_stat->bindColumn(1, $chatname);
$sq_stat->bindColumn(2,$username);
$sq_stat->bindColumn(3, $message);
$count=$sq_stat->execute();
答案 0 :(得分:4)
使用bindParam
或bindValue
而不是bindColumn
:
$sq_stat -> bindParam(1, $chatname);
$sq_stat -> bindParam(2, $username);
$sq_stat -> bindParam(3, $message);
答案 1 :(得分:3)
bindColumn
用于结果。您需要bindValue
或bindParam
$sq_stat->bindValue(1, $chatname);
$sq_stat->bindValue(2,$username);
$sq_stat->bindValue(3, $message);
答案 2 :(得分:0)
对于Insert语句,尝试将值作为数组传递给execute函数。
$db = new PDO($dns, $user, $pass);
$sql = "INSERT INTO `conversations_t` (`fk_crname_c`, `fk_username_c`, `message_c`) VALUES (?, ?, ?)";
$sq_stat = $db->prepare($sql);
$count = $sq_stat->execute(array($chatname, $username, $message));