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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看