如何使用 PHP::PDO 执行带有变量的 mysql 脚本?
我无法执行 pdo 引发异常的长脚本:
I am unable to execut long script the pdo throws an exception:
SQLSTATE[HY000]: General error
如果我提交不包含变量的脚本,它会运行没有问题.相同的脚本在 phpmyadmin 界面上运行.
If I submit script which does not contain variables it runs w/o problem. The same script runs on phpmyadmin interface.
这是我的代码片段:
try {
$dsn = "mysql:host=" . DB_SERVER . ";dbname=" . DB_DEFAULT;
$db = new PDO($dsn, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = $db->query($query);
if (!$q) {
echo $db->errorInfo();
} else {
$rows = $q->fetchAll(PDO::FETCH_ASSOC);
}
} catch (PDOException $e) {
var_dump($e);
}
这里是一些不由 PDO 执行的测试:
Here is some test which does not execute by PDO:
SET @ra_LMC:=80.9;
SELECT @ra_LMC;
我应该如何使用 pdo 执行多行脚本?
How I should execut with pdo the multi line scripts?
谢谢
阿尔曼.
推荐答案
PDO 不允许在一个 query() 请求中执行多个语句.但是您的@ra_LMC 变量应该在当前连接中可见,因此您可以将第二行 (SELECT) 放入新的 query() 调用中.
PDO does not allow the execution of multiple statements in one query() request. But your @ra_LMC variable should be visible in the current connection, so you can put your second line (SELECT) into a new query() call.
要读取整个脚本,您必须解析文件并通过调用 query() 运行每个语句.
To read a whole script, you have to parse the file and run each statement with a call to query().
相关文章