Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/194838-query-not-working/
Share on other sites

        $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.

Link to comment
https://forums.phpfreaks.com/topic/194838-query-not-working/#findComment-1024563
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/194838-query-not-working/#findComment-1024565
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.