hcdarkmage Posted July 14, 2010 Share Posted July 14, 2010 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???? Quote Link to comment https://forums.phpfreaks.com/topic/207733-select-query-caching/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/207733-select-query-caching/#findComment-1085945 Share on other sites More sharing options...
hcdarkmage Posted July 14, 2010 Author Share Posted July 14, 2010 Thank you for at least pointing me in the right direction. i looked up my problem and solved it. Again, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/207733-select-query-caching/#findComment-1085948 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.