cisclem Posted December 17, 2009 Share Posted December 17, 2009 I am having a problem retrieving the MAX value of a field in a MySQL table and I am not sure I am doing it correctly. My code is as follows (I have removed the connection details for obvious reasons)... $mysqli = mysqli_connect("", "", "", ""); $query = "SELECT MAX(ID) FROM stock"; $res = mysqli_query($mysqli, $query); The field 'ID' an an auto-increment field in the table 'stock' and I want to display the number of the last record. Can you please confirm if my SQL statement is correct and how to assign the result of the query to a variable that can be displayed - thanks Quote Link to comment https://forums.phpfreaks.com/topic/185492-problem-with-sql-max-statement/ Share on other sites More sharing options...
taquitosensei Posted December 17, 2009 Share Posted December 17, 2009 close if you're not looking for the id of the last record you entered $query="select MAX(ID) as MaxID from stock"; then you would just refer to MaxID if you're looking for the id of the last record you entered $query="select last_insert_id() as LastID from stock"; Quote Link to comment https://forums.phpfreaks.com/topic/185492-problem-with-sql-max-statement/#findComment-979337 Share on other sites More sharing options...
cisclem Posted December 17, 2009 Author Share Posted December 17, 2009 Thank you for that. I am pretty new to PHP so can you please tell me how to display the result - thanks Quote Link to comment https://forums.phpfreaks.com/topic/185492-problem-with-sql-max-statement/#findComment-979344 Share on other sites More sharing options...
taquitosensei Posted December 17, 2009 Share Posted December 17, 2009 that's a little more involved than a forum post. I'd suggest getting a book on using php and mysql try to get one that covers at the very least php 5.1 or finding a tutorial online. Quote Link to comment https://forums.phpfreaks.com/topic/185492-problem-with-sql-max-statement/#findComment-979397 Share on other sites More sharing options...
premiso Posted December 17, 2009 Share Posted December 17, 2009 To answer the question: $query="select last_insert_id() as LastID from stock"; $result = mysql_query($query) or trigger_error("Error in SQL: " . mysql_error()); $lastID = mysql_result($result, 0, 0); echo $lastID; Note that is how you obtain 1 field from the first row returned by the query, if you have multiple rows you want returned I would suggest looking into mysql_fetch_assoc (and notice the user comments for some usage). Quote Link to comment https://forums.phpfreaks.com/topic/185492-problem-with-sql-max-statement/#findComment-979418 Share on other sites More sharing options...
cisclem Posted December 18, 2009 Author Share Posted December 18, 2009 Thats great - thank you Quote Link to comment https://forums.phpfreaks.com/topic/185492-problem-with-sql-max-statement/#findComment-979653 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.