我使用this和this示例插入数据库,但我收到一条错误消息:在非对象上调用成员函数execute()。
如果我做INSERT INTO participants(email) VALUES ('$email')
evrething是好的。我的错误在哪里?
$pdo = new PDO('sqlite:participants.db');
$insert = $pdo->prepare("INSERT INTO participants(email) SELECT $email WHERE NOT EXISTS(SELECT 1 FROM participants WHERE email = $email);");
$insert->execute();
UPD:
这是var_dump($insert)
给出的内容:
object(PDOStatement)#2 (1) {
["queryString"]=>
string(65) "INSERT INTO participants (email) VALUES ('paekov@gmail.com')"
}
答案 0 :(得分:1)
看起来您的查询需要进行一些修改。
$pdo = new PDO('sqlite:participants.db');
$insert = $pdo->prepare("INSERT INTO participants(email) SELECT :email WHERE NOT EXISTS(SELECT 1 FROM participants WHERE email = :email)");
$insert->execute(array(':email' => $email));