nas
nas

Reputation: 2437

Raw query with IN and multiple where condition?

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

Answers (2)

Prabhjot Singh Kainth
Prabhjot Singh Kainth

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

Suresh Kamrushi
Suresh Kamrushi

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

Related Questions