挣扎着准备好的陈述

时间:2012-06-07 12:21:14

标签: php pdo prepared-statement

我无法完成以下工作。我想在下面的例子中使用预准备语句,但是我收到了一个错误。函数defnitily在$ array中传递正确的值:

  private function getInfoFromSystem($array) {
        try {
            $sql = "
                SELECT     
                    PCO_AGENT.NAME, 
                    PCO_INBOUNDLOG.LOGIN AS LOGINID, 
                    PCO_INBOUNDLOG.PHONE AS CALLERID, 
                    PCO_INBOUNDLOG.STATION AS EXTEN, 
                    PCO_INBOUNDLOG.TALKTIME AS CALLLENGTH, 
                    PCO_INBOUNDLOG.CHANNELRECORDID AS RECORDINGID, 
                    PCO_SOFTPHONECALLLOG.RDATE, 
                    PCO_INBOUNDLOG.RDATE AS INBOUNDDATE
                FROM         
                    PCO_INBOUNDLOG 
                INNER JOIN
                      PCO_LOGINAGENT ON PCO_INBOUNDLOG.LOGIN = PCO_LOGINAGENT.LOGIN 
                INNER JOIN
                      PCO_SOFTPHONECALLLOG ON PCO_INBOUNDLOG.ID = PCO_SOFTPHONECALLLOG.CONTACTID 
                INNER JOIN
                      PCO_AGENT ON PCO_LOGINAGENT.AGENTID = PCO_AGENT.ID
                WHERE
                    LOGINID = :extension
            ";
            $arr = array(":extension" => $array['extension']);
            $query = $this->mssql->prepare($sql);
            $query->execute($arr);
           // $sql = "select * from sys.messages where message_id = 229";
            foreach($this->mssql->query($sql) as $row) {

                echo "<pre>";
                print_r($row);
                echo "</pre>";
            }

        }
        catch(PDOException $e) {
            echo $e->getMessage();
        }
}

1 个答案:

答案 0 :(得分:1)

您不应在query()之后致电execute()。而是you need to fetch() your rows

$query = $this->mssql->prepare($sql);
$query->execute($arr);

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
  echo "<pre>";
  print_r($row);
  echo "</pre>";
}

// OR rather than the fetch loop above, use fetchAll()
$rowset = $query->fetchAll(PDO::FETCH_ASSOC);
print_r($rowset);

根据我对PDO + MSSQL的经验,尝试在返回行的语句上调用execute()后立即调用常规查询将失败,除非您先调用$query->closeCursor()。但是,在这种情况下,您根本不应该调用query()。你已经用绑定的params执行了你的语句,只需要从中获取行。