SELECT * FROM table WHERE IN()
ステートメントに配列を渡そうとしています。私は3つの結果を期待しています。代わりに2つの結果が得られました。php IN()へのバインド配列
私はまだ私は数字に十分なコードを理解しないので、PHPのPDOを学んでいますが、私は以下の2つのポスト
Can I bind an array to an IN() condition?
と
MySQL WHERE IN() + AND , PDO returns only one row
で認識して同じ質問していますコードを自分のコードに再利用する方法を説明します。
私が間違っていた箇所を誰かが指摘してくれることを願っています。なぜなら私はselect inステートメントから部分的な結果を得るからです。私は配列をIN()に正しく渡さなかったと思う。
は、以下の私のcontrolerページ
$customer_ids = array(1,2,3,4,5,6,7);
//in actual case I get array from another query.
$customer_id = implode(',',$customer_ids);
//add comma to the array
$template->orders = $rephome->getOrder($customer_id);
//passing the imploded array into my Model page with class named rephome,
//call on the method of getOrder, which is the select query
以下の私のモデルのページがあるのPHPコードです。以下は
public function getOrder($customer_id){
$this->db->query("SELECT * FROM orders WHERE customer_id_fk IN(.$customer_id.)");
$results = $this->db->resultset();
return $results;
私のデータベースクラスで
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null) {
if (is_null ($type)) {
switch (true) {
case is_int ($value) :
$type = PDO::PARAM_INT;
break;
case is_bool ($value) :
$type = PDO::PARAM_BOOL;
break;
case is_null ($value) :
$type = PDO::PARAM_NULL;
break;
default :
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue ($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
アレイ内の項目と同じ数のプレースホルダでPDO文字列を作成します。各プレースホルダを適切な値でバインドします。 – user2864740
いずれの場合でも、 "問題"は ".. IN(。$ customer_id。)" 'となり、' .. IN(.1,2,3,4,5,6,7。) 'となります。 SQLは無効です - '.'は文字列内の単なるリテラルです。エラーメッセージは、それほど多くのことを言っていました。 – user2864740
あなたは私の問題をちょうど解決したと思います。ドットを取り除くことによって。私は正しい結果を得た。 – codenoob