如何使用 PHP::PDO 执行带有变量的 mysql 脚本? [英] How to execute mysql script with variables using PHP::PDO?

查看:28
本文介绍了如何使用 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().

这篇关于如何使用 PHP::PDO 执行带有变量的 mysql 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆