使用SQLite3预准备语句时出现这些错误
警告:SQLite3 :: prepare():无法准备以下语句:1,“;”附近:第14行上的/Applications/MAMP/htdocs/tests/11/index.php中的语法错误
致命错误:在第15行的/Applications/MAMP/htdocs/tests/11/index.php中,调用布尔值上的成员函数bindValue()
我的代码是:
$db = new SQLite3('database.sqlite', SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
$db->query('CREATE TABLE IF NOT EXISTS "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"user_id" INTEGER,
"profile_photo" VARCHAR,
"reg_date" DATETIME)');
$statement = $db->prepare('INSERT INTO "users" ("user_id", "profile_photo", "reg_date") VALUES (;uid, ;profile_photo, ;reg_date)');
$statement->bindValue(';uid', 12);
$statement->bindValue(';profile_photo', 'usr12-e.jpg', SQLITE3_TEXT);
$statement->bindValue(';reg_date', date('Y-m-d H:i:s'));
$statement->execute();
答案 0 :(得分:0)
您的插入语句和bindValue中有错误。在编写准备好的变量的名称时,应使用冒号(:)而不是分号(;)。
这是正确的方法:
$statement = $db->prepare('INSERT INTO "users" ("user_id", "profile_photo", "reg_date") VALUES (:uid, :profile_photo, :reg_date)');
$statement->bindValue(':uid', 12);
$statement->bindValue(':profile_photo', 'usr12-e.jpg', SQLITE3_TEXT);
$statement->bindValue(':reg_date', date('Y-m-d H:i:s'));
$statement->execute();