I've been trying to implement an IN statment in my PDO sql. I've succesfully implemented a simple using the solution found in Using PDO with IN
My code is as follows:
$in_array = array("INTERNAL SITE","SUPPLIER");
$in = str_repeat('?,', count($in_array) - 1) . '?';
$dbname = $_SESSION['dbname'];
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $db->id, $db->pass);
//connect to db
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//error modes
$sql = "SELECT cs_id, name, alias, fk_cs_type as type, is_active as active
FROM companysite WHERE ( fk_cs_type IN ($in))";
$stmt = $conn->prepare($sql);
$stmt->execute($in_array);
However my end goal is to also implement CASE and Like clause referencing an additional 2 variables ($srchBy and $srchField). When I try to do this I get a HY093 error. What am I doing wrong in this code?
$in_array = array("INTERNAL SITE","SUPPLIER");
$in = str_repeat('?,', count($in_array) - 1) . '?';
$srchBy = "ALIAS";
$dbname = $_SESSION['dbname'];
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $db->id, $db->pass);
//connect to db
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//error modes
$sql ="SELECT cs_id, name, alias, fk_cs_type as type,
is_active as active FROM companysite
WHERE ( fk_cs_type IN ($in)) AND
((CASE WHEN $srchBy = 'ALIAS' THEN alias ELSE name END)
LIKE $srchField)";
$stmt = $conn->prepare($sql);
$stmt->execute($in_array);