将params传递给查询时,Php PDO无法正常工作?

时间:2014-11-26 05:00:50

标签: php jquery mysql methods pdo

我有以下php代码,我试图用php PDO执行相关查询,问题出在

WHERE :sno IN(:counselType) 

这是我的代码:

//--------------------- PHP code and Query with Array for passing the params ------------

$counselType = $_POST['counselType'];
    $counselType = $counselType == 1 ? "plaintiffAdvocateCC" : "defendantAdvocateCC";
    $ary = array(':sno'=>$_POST['counselSno'],':counselType'=>$counselType);
    //echo $counselType; print_r($bindVar);
    $sql = "SELECT DISTINCT c.caseSno,cs.caseNumber,cs.dateOriginalInstitution,CONCAT(cs.plaintiff,' V/S ',cs.respondant) AS caseTitle, cs.caseNature,cs.inTheCourt,
            c.dateOfHearing,c.isDecided,c.stageOfProceedings 
            FROM 
                chronologicallists c,cases cs 
            WHERE 
                :sno IN(:counselType) AND c.caseSno = cs.sno
            GROUP BY c.caseSno
            ORDER BY c.isDecided ASC";
    if($db->dbQuery($sql,$ary)){
        foreach($db->getRecordset($sql,$ary) as $row){
          //------- other code
               }
      }//if search

//------------------------------The Function in the class which handle the queries -------------
function for if search
public function dbQuery($sql,$bindVars=array()){
        try{
            $this->connect();
            $statement = $this->con->prepare($sql);
            $statement->execute($bindVars);
            if($statement->rowCount() > 0){
                return true;
                }
            else{
                return false;
                }
            }catch(PDOException $exc){
                $this->tempVar = $exc->getMessage();
                $this->sqlToReturn = $sql;
                return false;
                }
            $this->con = null;
        }   
---------------------------------------------------------------------
function for returning the RecordSet array

public function getRecordSet($sql,$bindVars=array()){
            try{
                $this->connect();
                $statement = $this->con->prepare($sql);
                $statement->execute($bindVars);
                return $statement->fetchAll();
            }catch(PDOException $ex){
                $this->tempVar = $ex->getMessage();
                return false;
                }               
            }//getRecordSet()

//----------------------------------------------------------------------------------------------

我不确定为什么这些参数不会被 PDO 翻译。 请指导我哪里出错了? 感谢

1 个答案:

答案 0 :(得分:2)

你必须动态地建立一个像这样的字符串

$list = ':foo1, :foo2, :foo3';

如果您的输入是

$array = ['a', 'b', 'c'];

然后你必须绑定:foo1到'a'等等

查询看起来像这样

WHERE sno IN (:foo1, :foo2, :foo3);

秒问题,:sno不可能,你不能绑定字段名称