如何使用PHP :: PDO使用变量执行mysql脚本?

时间:2021-04-17 01:11:55

I am unable to execut long script the pdo throws an exception:

我无法执行长脚本pdo抛出异常:

SQLSTATE[HY000]: General error

If I submit script which does not contain variables it runs w/o problem. The same script runs on phpmyadmin interface.

如果我提交的脚本不包含变量,它就会运行w / o问题。相同的脚本在phpmyadmin界面上运行。

Here is my code snippet:

这是我的代码片段:

 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);
 }

Here is some test which does not execute by PDO:

这是一些不由PDO执行的测试:

SET @ra_LMC:=80.9;
SELECT @ra_LMC;

How I should execut with pdo the multi line scripts?

我应该如何使用pdo执行多行脚本?

Thanks
Arman.

2 个解决方案

#1


11  

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.

PDO不允许在一个query()请求中执行多个语句。但是您的@ra_LMC变量应该在当前连接中可见,因此您可以将第二行(SELECT)放入新的query()调用中。

To read a whole script, you have to parse the file and run each statement with a call to query().

要读取整个脚本,您必须解析文件并通过调用query()来运行每个语句。

#2


6  

PDO can only execute one statement at a time. You can ether run the SET and SELECT as 2 separate statements. Or you can set the variable using FROM.

PDO一次只能执行一个语句。您可以将SET和SELECT作为2个单独的语句运行。或者您可以使用FROM设置变量。

SELECT @ra_LMC FROM (SELECT @ra_LMC:=80.9) q

#1


11  

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.

PDO不允许在一个query()请求中执行多个语句。但是您的@ra_LMC变量应该在当前连接中可见,因此您可以将第二行(SELECT)放入新的query()调用中。

To read a whole script, you have to parse the file and run each statement with a call to query().

要读取整个脚本,您必须解析文件并通过调用query()来运行每个语句。

#2


6  

PDO can only execute one statement at a time. You can ether run the SET and SELECT as 2 separate statements. Or you can set the variable using FROM.

PDO一次只能执行一个语句。您可以将SET和SELECT作为2个单独的语句运行。或者您可以使用FROM设置变量。

SELECT @ra_LMC FROM (SELECT @ra_LMC:=80.9) q