When this query is executed I get the response (through AJAX) I expect. It works.
$stmt = $pdo->prepare("SELECT item_location AS Location, users_description AS Description, price AS Price FROM sale_items WHERE item_type IN ($cs_vals)”);
But when I add another table column name to the WHERE clause like so:
$stmt = $pdo->prepare("SELECT item_location AS Location, users_description AS Description, price AS Price FROM sale_items WHERE (item_type, map_region) IN ($cs_vals)”);
....I get a 'JSON.parse: unexpected character....' message in the console and nothing displayed in my webpage. Why is this? What am I doing wrong here. Thanks in advance for any help. BTW, $cs_vals is a list of comma separated values in the format ('a', 'b', 'c'...). This comma separated list has been generated by
$cs_vals = str_repeat('?,', count($arr) - 1) '?';
The $arr variable is an array of non NULL values extracted from a user generated form.
I am assuming you know about the FROM
You have to check one at a time:
WHERE ((item_type) IN ($cs_vals)
OR (map_region) IN ($cs_vals))
Unless you are trying to find the combo in an array.
You use EXISTS
and instead of $cs_vals
you create an inline table using UNION
so you can validate if the tuple match your list.
SELECT item_location AS Location, users_description AS Description, price AS Price
FROM sale_items
WHERE EXISTS (SELECT 1
FROM (SELECT 'A' as item_type, 1 as map_region
UNION
SELECT 'B' as item_type, 2 as map_region
) as T
WHERE sale_items.item_type = T.item_type
AND sale_items.map_region = T.map_region
)
Also you can use IN but need compare with tuples like this:
SELECT item_location AS Location, users_description AS Description, price AS Price
FROM sale_items
WHERE (item_type, map_region) IN (('A',1),('B',2));