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
https://forums.phpfreaks.com/topic/207733-select-query-caching/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.