jamesxg1 Posted March 10, 2010 Share Posted March 10, 2010 Hiya! I have this code. $newID = "SELECT `id` FROM `jobs` ORDER BY `id` DESC"; $getID = mysql_query($newID); if($getID): while($nid = mysql_fetch_assoc($getID)): $id = $nid['id']; endwhile; if($id == 0): $id = 1; endif; else: return 'Failed to complete your request to open a new job, please contact support.'; endif; But its returning 1 everytime, even when the MySQL database has ID's like (1,2,3,4,5,6) ect. It should be adding +1 on the highest ID. Many thanks James Quote Link to comment https://forums.phpfreaks.com/topic/194838-query-not-working/ Share on other sites More sharing options...
jamesxg1 Posted March 11, 2010 Author Share Posted March 11, 2010 B to the U to the M to the P. Quote Link to comment https://forums.phpfreaks.com/topic/194838-query-not-working/#findComment-1024505 Share on other sites More sharing options...
jamesxg1 Posted March 11, 2010 Author Share Posted March 11, 2010 BUMP Quote Link to comment https://forums.phpfreaks.com/topic/194838-query-not-working/#findComment-1024560 Share on other sites More sharing options...
jdorma0 Posted March 11, 2010 Share Posted March 11, 2010 $newID = "SELECT `id` FROM `jobs` ORDER BY `id` LIMIT 1"; $getID = mysql_query($newID); if($getID): while($nid = mysql_fetch_assoc($getID)): $id = $nid['id']; endwhile; if($id == 0): $id = 1; endif; else: return 'Failed to complete your request to open a new job, please contact support.'; endif; Give that a whirl. Quote Link to comment https://forums.phpfreaks.com/topic/194838-query-not-working/#findComment-1024563 Share on other sites More sharing options...
mikesta707 Posted March 11, 2010 Share Posted March 11, 2010 its doing that because your while loop goes through all the ids in descending order, and overwrites your $id variable with the latest row gotten from the mysql_fetch_assoc() function. At the end, (assuming your first id is 0) the last row checked is the row with an id of 0, which is the last time $id is overwritten, so it ends up being 0 (which makes your if statement true) jdorms code will make it stop doing that, but what exactly are you trying to accomplish with your code? Based on your OP, and the code, I can't really tell Quote Link to comment https://forums.phpfreaks.com/topic/194838-query-not-working/#findComment-1024565 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.