Re: Re: [suspicious - maybe spam] [PHP] [suspicious - maybe spam] SELECT probrem
| From: | Josh Whiting | Date: | Fri, 07 Jan 2005 00:46:57 +0000 |
| Subject: | Re: Re: [suspicious - maybe spam] [PHP] [suspicious - maybe spam] SELECT probrem | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-205796@lists.php.net to get a copy of this message | ||
> Hello Phpu,
>
> Thursday, January 6, 2005, 10:42:15 AM, you wrote:
>
> P> I have an array, for ex: $products=array(1, 2, 5 , 7)
> P> I want to select all products from the database that has the ids of products.
> P> I use this but doesn't work:
>
> $product_ids = implode(',', $products);
> $sql = "SELECT product_name FROM accessories WHERE product_id IN ($product_ids)";
>
> Best regards,
>
> Richard Davey
Slightly off topic but worth mentioning is:
If your $products array is generated dynamically, make sure it isn't
empty before running the query, MySQL does NOT like:
"SELECT product_name FROM accessories WHERE product_id IN ()"
The empty "()" will cause a MySQL error (it won't just return an empty
result set.) Plus, why run the query if the array is empty anyway?
/jw