-2

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;
    }   
}

この場合、ユーザーが日付範囲を入力しなかった場合、バインディング パラメーターは機能せず、エラーが発生します。

4

2 に答える 2

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;
}   
于 2013-07-15T16:51:01.490 に答える
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;
    }   
}
于 2013-07-15T17:24:17.367 に答える