PDO将SQL查询与占位符连接在一起

时间:2013-07-15 16:35:36

标签: php pdo

我正在将我的mysql _。*函数转换为PDO。在此之前,我将基于其他因素连接它来构建我的查询。这是一个例子:

class carriersInfo
{

    protected $start_date;
    protected $end_date;
    protected $data = array();
    public function __construct($start = 0, $end = 0)
    {
        $this->start_date = $start;
        $this->end_date = $end;
        $this->topCarrier();
    }

    protected function buildQ()
    {
        $sql =  "SELECT `shipped_by`, COUNT(`shipped_by`) AS `total` FROM `deliveries` ";
        if($this->start_date!=0 and $this->end_date!=0)
        {
            $sql .= " WHERE ship_date>='".$this->start_date."' AND ship_date<='".$this->end_date."' ";
        }   
        $sql .= " GROUP BY `shipped_by` ";
        $sql .= " ORDER BY total ASC "; 
        return $sql;    
    }

    public function topCarrier()
    {       
        $query = $this->buildQ();
        $SQL = mysql_query($query) or die(mysql_error());
        while($data = mysql_fetch_array($SQL))
        {
            $this->data[$data['shipped_by']] = $data['total'];
        }
        return $this->data;
    }   
}

根据用户是否选择指定日期范围,构建SQL查询。如何使用PDO预处理语句和名称持有者实现相同的效果?

尝试

class carriersInfo
{

    protected $start_date;
    protected $end_date;
    protected $data = array();
    protected $_INSTANCE;
    public function __construct($start = 0, $end = 0)
    {
        $this->_INSTANCE = Core::getInstance();
        $this->start_date = $start;
        $this->end_date = $end;
        $this->topCarrier();
    }

    protected function buildQ()
    {
        $sql =  "SELECT `shipped_by`, COUNT(`shipped_by`) AS `total` FROM `deliveries` ";

        if($this->start_date!=0 and $this->end_date!=0)
        {
            $sql .= " WHERE ship_date>=:start_date AND ship_date<=:end_date ";
        }   
        $sql .= " GROUP BY `shipped_by` ";
        $sql .= " ORDER BY total ASC "; 



        return $sql;    
    }

    public function topCarrier()
    {       
        $Q = $this->buildQ();
        $query = $this->_INSTANCE->pdo->prepare($Q);
        $query->bindValue(":start_date",$this->start_date);
        $query->bindValue(":end_date",$this->end_date);
        $query->execute();  

        while($data = $query->fetch())
        {
            $this->data[$data['shipped_by']] = $data['total'];
        }
        return $this->data;
    }   
}

如果用户没有输入日期范围并且会导致错误,则绑定参数在这种情况下将不起作用。

2 个答案:

答案 0 :(得分:2)

如果我理解正确,尝试绑定不存在的参数会导致查询失败。基于此,您应该只添加对这些属性的检查:

public function topCarrier()
{       
    $Q = $this->buildQ();
    $query = $this->_INSTANCE->pdo->prepare($Q);
    if($this->start_date!=0 and $this->end_date!=0) {
        $query->bindValue(":start_date",$this->start_date);
        $query->bindValue(":end_date",$this->end_date);
    }
    $query->execute();  

    while($data = $query->fetch())
    {
        $this->data[$data['shipped_by']] = $data['total'];
    }
    return $this->data;
}   

答案 1 :(得分:1)

想出来。不知道你可以执行一系列参数。这是我的解决方案。

class carriersInfo
{

    protected $start_date;
    protected $end_date;
    protected $data = array();
    protected $_INSTANCE;
    protected $_PARAMETERS = array();

    public function __construct($start = 0, $end = 0)
    {
        $this->_INSTANCE = Core::getInstance();
        $this->start_date = $start;
        $this->end_date = $end;
        $this->topCarrier();
    }

    protected function buildQ()
    {
        $sql =  "SELECT `shipped_by`, COUNT(`shipped_by`) AS `total` FROM `deliveries` ";

        if($this->start_date!=0 and $this->end_date!=0)
        {

            $sql .= " WHERE ship_date>=:start_date AND ship_date<=:end_date ";
            $this->_PARAMETERS[":start_date"] = $this->start_date;
            $this->_PARAMETERS[":end_date"] = $this->end_date;
        }   
        $sql .= " GROUP BY `shipped_by` ";
        $sql .= " ORDER BY total ASC "; 
        return $sql;    
    }

    public function topCarrier()
    {       
        $Q = $this->buildQ();
        $query = $this->_INSTANCE->pdo->prepare($Q);
        $query->execute($this->_PARAMETERS);    

        while($data = $query->fetch())
        {
            $this->data[$data['shipped_by']] = $data['total'];
        }
        return $this->data;
    }   
}