php和sqlite insert语句的语法问题

时间:2016-04-05 14:56:17

标签: php sqlite

当我尝试插入Cat和Purr时,此代码给出了一个错误。该教程没有双引号,但它们的代码也不起作用。这必须是某种语法问题。

message('creating the db Object');
    $db = new bwSQLite3(DB_FILENAME, TABLE_NAME);
    $tn = TABLE_NAME;
    message('creating the table');
    $db->sql_do("Drop table if exists $tn");
    $db->sql_do("create table $tn ( id integer primary key, animal text, sound text )");
    //insert some records
    $db->sql_do("insert into $tn (animal, sound) values (?, ?), 'cat', 'Purr' "); //right here issues
    $db->sql_do("insert into $tn (animal, sound) values (?, ?), 'dog', 'Woof' ");
    $db->sql_do("insert into $tn (animal, sound) values (?, ?), 'duck', 'Quack' ");
    $db->sql_do("insert into $tn (animal, sound) values (?, ?), 'bear', 'Grr' ");
    message('there are %d rows in the table', $db->count_recs());
}catch (PDOException $e) {
    error($e->getMessage());
}

这是错误: 解析错误:语法错误,第42行的C:\ xampp \ htdocs \ ExerciseFiles \ Chap01 \ create.php中的意外“插入”(T_STRING)

1 个答案:

答案 0 :(得分:1)

 $db->sql_do("insert into $tn (animal, sound) values (?, ?), 'cat', 'Purr' "); //right here issues

应该是

 $db->sql_do("insert into $tn (animal, sound) values (?, ?)", 'cat', 'Purr'); 

查看语法高亮。

听起来你似乎并不了解PHP如何使用字符串,试着看看差异:

$my_name = 'Matt';
echo 'Hello $my_name.';
echo "Hello $my_name.";