据我所知,在PHP5.3中,PDO_MYSQLND
取代了PDO_MYSQL
.令人困惑的是,这个名字仍然是PDO_MYSQL
.现在ND是MySQL+PDO的默认驱动程序.
总的来说,要一次执行多个查询,您需要:
- PHP5.3+
- mysqlnd
- 模仿事先准备好的陈述.确保将
PDO::ATTR_EMULATE_PREPARES
设置为1
(默认).或者,您可以避免使用预先准备好的语句,直接使用$pdo->exec
.
Using exec个
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
// works regardless of statements emulation
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
$sql = "
DELETE FROM car;
INSERT INTO car(name, type) VALUES ('car1', 'coupe');
INSERT INTO car(name, type) VALUES ('car2', 'coupe');
";
$db->exec($sql);
Using statements
$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
// works not with the following set to 0. You can comment this line as 1 is default
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$sql = "
DELETE FROM car;
INSERT INTO car(name, type) VALUES ('car1', 'coupe');
INSERT INTO car(name, type) VALUES ('car2', 'coupe');
";
$stmt = $db->prepare($sql);
$stmt->execute();
注:
当使用模拟的准备好的语句时,确保在DSN中设置了正确的编码(反映实际的数据编码)(从5.3.6开始提供).否则there can be a slight possibility for SQL injection if some odd encoding is used.