Jump to content

SELECT query caching?


hcdarkmage

Recommended Posts

Okay, so I was on here and I figured the problem out for my insert query, but this one really baffles me.

 

I have a SELECT query that is supposed to retrieve the last contact id so that I may add 1 to it, but for some reason it isn't working. I tried it in my query editor and it works fine, but this is just strange. Any help on this would be greatly appreciated.

 

The last ID, as it is now is 138.

$getNum = "SELECT contact_id FROM contact_form2 ORDER BY contact_id DESC LIMIT 1";
$contactID = mysql_query($getNum) or die(mysql_error());

echo "<!-- ".$contactID." is the retrieved ID. -->";  //Testing code

$contactid = $contactID+1;

echo "<!-- ".$contactid." is the Contact ID. -->";  //Testing code

 

When I run the script I check the source and I see this:

<!-- Resource id #137 is the retrieved ID. --><!-- 138 is the Contact ID. -->

 

Can anyone help me with this problem????

Link to comment
Share on other sites

mysql_query() returns a result resource. It does not return the SELECTed value. You must fetch the value from the result set that the result resource points to.

 

However, you cannot reliably SELECT the highest value and add one to it because if there are concurrent visitors and concurrent queries, you cannot guarantee the order in which queries execute (unless you lock the table.)

 

You would typically either use an auto-increment column and let the database do this or you would do it using a single UPDATE query.

Link to comment
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.