I am trying to pass an array into a SELECT * FROM table WHERE IN()
statement. I am expecting 3 result. but I got 2 result instead.
I am aware same question in the below 2 posts
Can I bind an array to an IN() condition?
and
MySQL WHERE IN () + AND , PDO returns only one row
However I am still learning php pdo, thus I dont understand the code enough to figure out how to repurpose their code to my code.
I hope someone can point out where I did wrong. because I get partial results from the select in statement. I suspect I did not pass array into the IN() correctly.
below is php code of my controler page
$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
below is my model page.
public function getOrder($customer_id){
$this->db->query("SELECT * FROM orders WHERE customer_id_fk IN(.$customer_id.)");
$results = $this->db->resultset();
return $results;
Below is what is in my database class
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);
}