Reputation: 2437
I need to write a query with a IN
condition and multiple WHERE
condition. I have tried following SQL statement but this one is not getting me result. Maybe I did something wrong.
SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
$columns = "DISTINCT model, manufacturer, logo, year, update_status";
$modelList = array('Version', 'Facelift', 'Optionsänderung');
$orderBy = "manufacturer, model";
// Prepare the sql statement.
$sql = "select $columns from tbl_manufacture where model IN (:modelList) year > ? AND update_status = ?";
$sql.="order by $orderBy";
$em = $this->getEntityManager();
$query = $em->getConnection()
->prepare($sql);
$query->bindValue("modelList", $modelList, Connection::PARAM_STR_ARRAY);
$params = array(
"year" => $year,
"update_status" => $status
);
// Execute the statement
$query->execute($params);
Upvotes: 1
Views: 107
Reputation: 1861
You need to convert your array in comma separated string to use it IN query like this:
$modelList = array('Version', 'Facelift', 'Model');
$modelList = "'" . implode ( "', '", $modelList ) . "'";
Result: 'Version', 'Facelift', 'Model'
Also you are missing AND
after IN (:modelList)
Upvotes: 1
Reputation: 16086
You are missing "AND" after IN condition:
$sql = "select $columns from tbl_manufacture where model IN (:modelList) AND year > ? AND update_status = ?";
$sql.="order by $orderBy";
Upvotes: 2