simonjk Posted September 17, 2012 Share Posted September 17, 2012 Hi, I have a table in which one of the columns is called ID. This is an auto-increment column which assigns a new ID everytime a users joins (i.e.a new row is created in the table). Now, I need to send emails to the users and have some code written to do this. The problem is that if the ID is not recognised the code stops. What I would like the code to do it: 1. Set the ID number (starting from 1) 2. Find the maximum ID number (i.e. the highest number assigned) 3. Keep on incrementing through the ID numbers until the number is greater than the maximum ID number and then stop. Here's part of what I have so far but it does not seem to work and the code stops as soon as an ID is not found. At the ops of the code I have ... $maxusers=mysql_query("SELECT MAX(ID) FROM `awisemails`"); $usercount=0; { do{$usercount++;.... (I then count through the users) . . . } while ($usercount <= $maxusers); However, I must be dong something wrong? Wondered if anyone can spot it? Thanks, Simon Quote Link to comment Share on other sites More sharing options...
spiderwell Posted September 17, 2012 Share Posted September 17, 2012 Why not just select the rows from the DB, and do a normal recordset WHILE loop?? You wont encounter any invalid IDs (theoretically). Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 17, 2012 Share Posted September 17, 2012 You should be able to just select all users, if you need to email every user. Are you using a class like PHPMailer? Quote Link to comment Share on other sites More sharing options...
simonjk Posted September 17, 2012 Author Share Posted September 17, 2012 Thanks Spiderwell...I have to select some data off another table too and use the ID to do this, hence using the ID. Jesirose, I am not selecting all the users to email as there is then a condition from another table (it's actually the area they want to receive a weather forecast for). Thanks for your replies though. Simon Quote Link to comment Share on other sites More sharing options...
simonjk Posted September 17, 2012 Author Share Posted September 17, 2012 I guess what I want it to do is to say 'if you don't find an ID, and the number is still below the maximum in ID then go back to 'do', add another 1 and keep going until the ID is greater than the maximum ID'. I hope this makes sense? Simon Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 17, 2012 Share Posted September 17, 2012 Use a join then. This is easier than you're making it. Quote Link to comment Share on other sites More sharing options...
simonjk Posted September 17, 2012 Author Share Posted September 17, 2012 Okay, thanks, now need to learn about joins. Simon Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 17, 2012 Share Posted September 17, 2012 If you post your table structure and describe what you're trying to do, we can help if you get stuck on your join. Good luck. Quote Link to comment Share on other sites More sharing options...
simonjk Posted September 17, 2012 Author Share Posted September 17, 2012 Thanks, will try to compress the table. Before that though, have you any idea why this won't work? How can I just get it to ignore missing ID's so long as they are less than the highest ID in the table column? $maxusers=mysql_query("SELECT MAX(ID) FROM `awisemails`"); $usercount=0; { do{$usercount++;.... (I then count through the users) . . . } while ($usercount <= $maxusers); Thanks, Simon Quote Link to comment Share on other sites More sharing options...
spiderwell Posted September 17, 2012 Share Posted September 17, 2012 if its missing it should ignore it as it wont be there?? or am I missing something here? I think you are making this harder for yourself than you need to!!! a recordset will contain the IDs that you have asked for, and using a while loop it will loop from start to end, whether its 5 records with ID 1 to 5 or 1000 records with ID from 1 to 1000. If could even be a number inbetween , without being incremental all the time due to missing/deleted rows. like ID 50 to 250 but not having 175 to 200 You dont need a usercount, you dont need a max users, unless I have misunderstood greatly what is going on Quote Link to comment Share on other sites More sharing options...
simonjk Posted September 18, 2012 Author Share Posted September 18, 2012 Thanks guys, You were spot on, no need for ID's...have it all working now. Thanks for your help when I could not see the wood for the trees! Simon Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.