问题描述
我无法执行长脚本,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?
谢谢
阿尔曼.
Thanks
Arman.
推荐答案
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().
这篇关于如何使用PHP :: PDO使用变量执行mysql脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!