isedeasy Posted November 25, 2009 Share Posted November 25, 2009 I need to increment a value in a row of my table, I also want to get that value. mysql_query("UPDATE deals SET likes = likes + 1 WHERE id = '$deal'"); How would I get the value of 'likes' without doing a separate select query? Cheers for any help. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/ Share on other sites More sharing options...
fenway Posted November 25, 2009 Share Posted November 25, 2009 Read the refman on LAST_INSERT_ID() with with a parameter -- very cool, mostly unknown functionality, explicitly for this purpose. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-965725 Share on other sites More sharing options...
isedeasy Posted November 26, 2009 Author Share Posted November 26, 2009 Hi Cheers for the pointer, I had a look at the manual but I can't quite get my head around it, do I still need to-do select query. Sorry I am very new to php and mysql and I want to make sure I use functions correctly. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-965907 Share on other sites More sharing options...
fenway Posted November 26, 2009 Share Posted November 26, 2009 Yes, you'll need to run "SELECT LAST_INSERT_ID()" afterwards, but not a true mysql query. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-965916 Share on other sites More sharing options...
isedeasy Posted November 28, 2009 Author Share Posted November 28, 2009 Im still having problems I feel so stupid I tried the following but it does not work $likes = mysql_query("SELECT LAST_INSERT_ID(likes)"); Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-967154 Share on other sites More sharing options...
fenway Posted November 29, 2009 Share Posted November 29, 2009 Show me the update statement. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-967468 Share on other sites More sharing options...
isedeasy Posted November 29, 2009 Author Share Posted November 29, 2009 mysql_query("UPDATE deals SET likes = likes + 1 WHERE id = '$deal'"); Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-967705 Share on other sites More sharing options...
fenway Posted November 29, 2009 Share Posted November 29, 2009 Well, you're not using LAST_INSERT_ID() in your UPDATE statement, so of course it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-967716 Share on other sites More sharing options...
isedeasy Posted November 29, 2009 Author Share Posted November 29, 2009 I would not normally ask but this is starting to-do my head in lol Could you write an example of how to use the function? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-967767 Share on other sites More sharing options...
Philip Posted November 30, 2009 Share Posted November 30, 2009 If I understand it correctly, you're wanting to basically combine your UPDATE/SELECT queries... Currently' date=' you cannot update a table and select from the same table in a subquery.[/quote'] Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-967935 Share on other sites More sharing options...
fenway Posted November 30, 2009 Share Posted November 30, 2009 Try this: mysql_query("UPDATE deals SET likes = LAST_INSERT_ID(likes + 1) WHERE id = '$deal'"); Followed by: $likes = mysql_query("SELECT LAST_INSERT_ID()"); Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-968459 Share on other sites More sharing options...
isedeasy Posted November 30, 2009 Author Share Posted November 30, 2009 When I try that $like returns Resource id #8 Cheers for your help, I appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-968569 Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 $res = mysql_query("SELECT LAST_INSERT_ID()"); $likes = mysql_fetch_row($res); $likes = $likes[0]; echo $likes; You have to fetch the data to display it as seen above. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-968577 Share on other sites More sharing options...
isedeasy Posted November 30, 2009 Author Share Posted November 30, 2009 Getting there That returns the value of 'likes' twice eg if likes = 10, I get 1010 Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-968579 Share on other sites More sharing options...
fenway Posted December 1, 2009 Share Posted December 1, 2009 That's hard to believe... the sql works. Quote Link to comment https://forums.phpfreaks.com/topic/182958-update-then-select/#findComment-968857 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.