我准备的pdo插入语句出错

时间:2013-09-17 12:48:31

标签: php mysql pdo

我正在尝试使用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();

3 个答案:

答案 0 :(得分:4)

使用bindParambindValue而不是bindColumn

$sq_stat -> bindParam(1, $chatname);
$sq_stat -> bindParam(2, $username);
$sq_stat -> bindParam(3, $message);

答案 1 :(得分:3)

bindColumn用于结果。您需要bindValuebindParam

$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));