In a php page, I have an array, similar to this:
$category = array(16, 22, 23);
Then I am doing a database query with a prepared statement. I would like to get all rows where the field category
contains one of the values from that $category
array and where price
is lower than a value stored in the variable $price
.
Among others I read the answers to this question and tried to use find_in_set()
as described there (and at a lot of other places), but somehow I can't make it work within the prepared statement. I tried this:
/* database connection "$db" is established beforehand and works */
if($ps = $db->prepare("
SELECT id, product, category, price
FROM products
WHERE price <= ? and find_in_set(?, category)
ORDER BY id") {
$ps->bind_param("ds", $price, $category);
$ps->execute();
$ps->bind_result($id, $name, $cat, $pr);
while($ps->fetch()) {
/* ... echo the results ..... */
}
$ps->free_result();
$ps->close();
}
But I get an empty result.
If I try to use "dd" or "di" instead of "ds" in the bind_param()
line, I do get results, but the wrong ones - I get all rows with category 1.
I also tried to use category IN ?
instead of find_in_set(?, category)
, but that won't work either.
What can I do to make that work? Any help appreciated!