doulu4413 2013-03-04 16:58
浏览 18
已采纳

PDO多个查询无法正常工作

I am using PDO to run a query, and it isn't working how I would like. I wrote a wrapper around pdo so, and here is the main query method:

<?php
public function query($query, $params = array())
{
    if(!is_array($params))
    {
        throw new Exception("Parameter 2 must be a key => value array.");
    }
    if(!$this->pdo)
    {
        $this->connect();
    }
    echo "
--------------------------------------------------------------------------------

$query

--------------------------------------------------------------------------------    
    ";
    $this->sql = $this->pdo->prepare($query);
    foreach($params as $key => $value)
    {
        $this->sql->bindParam($key, $value);
    }
    $sql = $this->sql->execute();
    if(!$sql)
        throw new Exception('[' . $this->sql->errorCode() . ']: ' . $this->sql->errorInfo());
}

I can not show the whole query, but here are parts of it:

    $str = "SET @usapev = :usapev;
    SET @canpev = :canpev;
    SET @ven = :ven;
    SET @usa = :isUSA;
    SET @can = :isCAN;

    DROP TEMPORARY TABLE IF EXISTS T_MailingList;
    CREATE TEMPORARY TABLE T_MailingList
    (
        bsg_uk INT NOT NULL PRIMARY KEY,
        demog_id INT NOT NULL,
        procardnbr VARCHAR(12)
    );

    INSERT IGNORE INTO T_MailingList
    SELECT m.bsg_uk
    , m.demog_id
    , m.procardnbr
    FROM FROM bsg.member m;";

I then run the query like so:

<?php
    $this->db->query($str, array(
        "ven"    => $ven,
        "usapev" => $usapev,
        "canpev" => $canpev,
        "isUSA"  => (int)$is_usa,
        "isCAN"  => (int)$is_can
    ));

When the query is echoed out in the echo in query() I copy that to workbench and run it, and it works fine in there. No errors, and I get results back. In pdo when I run another query that wants to use the temporary table, the table doesn't exist.

Why is it not working in PDO?

  • 写回答

2条回答 默认 最新

  • dsieyx2015 2013-03-04 17:04
    关注

    Use

    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
    

    or explode the query with ";" and run each query in a loop

    $queries = explode(";", $query);
    foreach ($queries as $query) {
        $pdo->query($query, $attrs);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写