Let's say in a scenario: $ids = 2,3
. But for some reason the records are getting returned as if: $ids = 2
.
I believe there's some problem in this line from the complete code, because when I echo $ids
, it returns 2,3
, but the actual query returns as if there's only one id. I.e. it returns events from one course only (of id 2).
$statement->execute(array("ids" => $ids, 'timestart' => $timestart, 'timeend' => $timeend))) {
my full code:
$ids = array_map(function($item) { return $item->id; }, $entitlementsVOs);
$ids = implode(', ', $ids); //echo shows 2,3
$timestart = isset($_GET['start']) && $_GET['start'] != "" ? $_GET['start'] : null;
$timeend = isset($_GET['end']) && $_GET['end'] != "" ? $_GET['end'] : null;
$statement = $this->connection->prepare("SELECT name AS title, timestart AS start, timestart + timeduration AS end FROM event WHERE courseid IN(:ids) AND timestart >= :timestart AND timestart + timeduration <= :timeend");
$statement->setFetchMode(\PDO::FETCH_CLASS, get_class(new EventVO()));
if($statement->execute(array("ids" => $ids, 'timestart' => $timestart, 'timeend' => $timeend))) {
return $statement->fetchAll();
} else {
return null;
}
p.s. manually running this query in mysql workbench returns my two records (1 from course id 2 and the other from course 3) where I substitute in operator with (2,3), but in php execution I get only one record back.
If I hard code the values in php courseid IN(2,3)
, e.g.:
$statement = $this->connection->prepare("SELECT name AS title, timestart AS start, timestart + timeduration AS end FROM event WHERE courseid IN(2,3) AND timestart >= :timestart AND timestart + timeduration <= :timeend");
I get what I'm expecting, so I believe there's some problem with either implode
, or IN
operator or $statement->execute
.
Edit:
I've read the dupe, but I don't know where to start with the named placeholders to accomplish the same. My question is when I've both named parameters and IN operator, the dupe uses IN operator only with position placeholders.
Edit 2
I've read the second dupe, it's not related, my question uses a mix of both IN and named placeholders, the linked question doesn't address that, however, I've got the solution now in the answer.