tags:

views:

49

answers:

1

hi,

i have a problem with a query in a php file. I have a query that retrieves an e-mail address, it works about right, but when it sends the email, the address is added twice.

SELECT email FROM jos_users WHERE id=$i

$i is a number that auto increments.

i have a for to check each user id to see if a email should be sent.

Any ideas?

thanks, Sebastian

+2  A: 

If your list of ids is not too long, you could use something like this:

SELECT DISTINCT email FROM jos_users WHERE id IN (5, 18, 24, ...);

If you get a packet too long error, you can either increase the packet size, use a temporary table or keep track of the emails in your PHP application. Here is an example using temporary tables:

CREATE TEMPORARY TABLE tmp (id INT);
/* insert first 50 values (or 100, or 1000, whichever fits into your packet size)*/
INSERT INTO tmp (id) VALUES (5), (18), (24) ...;
/* insert next 50 values */
INSERT INTO tmp (id) VALUES (105), (118), (124) ...;
/* more inserts until you have a temporary table containing all ids */
INSERT INTO tmp ...
INSERT INTO tmp ...
/* select */
SELECT DISTINCT email FROM jos_users INNER JOIN tmp USING (id);
soulmerge