我正在创建一个处理各种SQLite操作的类.我的问题是:当我创建一个包含多个语句的SQL字符串时,它在使用标准PHP => $ db-> query()时起作用...但是当从方法发出相同的请求时它会失败.似乎OO方法在我的SQL语句中第一个";" - 符号之后跳过所有内容.为什么这样,以及如何解决它?
谢谢.
// Fails - line 2 is not inserted, why? $this->db_sqlite->query(" INSERT INTO foo (name) VALUES('Via class multi-lines 1'); INSERT INTO foo (name) VALUES('Via class multi-lines 2'); "); // Works - both lines are inserted. $GLOBALS[db]->query(" INSERT INTO foo (name) VALUES('Direct multi-lines 1'); INSERT INTO foo (name) VALUES('Direct multi-lines 2'); ");
完整示例:
connect(); } function connect() { $GLOBALS[db] = new SQLiteDatabase("dbsqlite.php.db"); } function query($sql) { return $GLOBALS[db]->query($sql); } } class something { function setup() { $this->db_sqlite = new db_sqlite(); $this->db_sqlite->query("CREATE TABLE foo ( id INTEGER PRIMARY KEY, name CHAR(255) );"); // Works $this->db_sqlite->query("INSERT INTO foo (name) VALUES('Via class one line 1');"); $this->db_sqlite->query("INSERT INTO foo (name) VALUES('Via class one line 2');"); // Fails (why?) $this->db_sqlite->query(" INSERT INTO foo (name) VALUES('Via class multi-lines 1'); INSERT INTO foo (name) VALUES('Via class multi-lines 2'); "); // Works $GLOBALS[db]->query(" INSERT INTO foo (name) VALUES('Direct multi-lines 1'); INSERT INTO foo (name) VALUES('Direct multi-lines 2'); "); foreach($this->db_sqlite->query("SELECT * FROM foo") as $v) { echo $v[id] . " - " . $v[name] ."
"; } } } $something = new something(); $something->setup(); ?>
输出:
1 - 通过第1类线1(正确)
2 - 通过第1类线2(正确)3 - 通过类多线1(不正确)
4 - 直接多线1(正确)
5 - 直接多线2 (正确)
sqlite_query()的PHP手册页说,关于在一个函数调用中使用多个语句,"...仅当未使用函数的结果时才起作用 - 如果使用它,则只执行第一个SQL语句. " 由于您返回结果,因此您正在"使用"它.无论如何,这是我的猜测.