問題描述
我無法執行 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().
這篇關于如何使用 PHP::PDO 執行帶有變量的 mysql 腳本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!