monkeybidz Posted September 6, 2010 Share Posted September 6, 2010 How do I call the last ID number from a table in MySql and echo it? This is what I have so far: $query = mysql_query("SELECT idnum FROM ready_aclassft"); Maybe im comeing at it all wrong. :'( Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/ Share on other sites More sharing options...
Rifts Posted September 6, 2010 Share Posted September 6, 2010 SELECT * FROM Persons ORDER BY age DESC Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1107975 Share on other sites More sharing options...
monkeybidz Posted September 6, 2010 Author Share Posted September 6, 2010 Can I see more code? That got me a result of S instead of a number. Thanks in advance, Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1107978 Share on other sites More sharing options...
systemick Posted September 6, 2010 Share Posted September 6, 2010 $result = mysql_query("SELECT MAX(idnum) as id from ready_aclassft"); $row = mysql_fetch_row ($result ); echo $row['id']; Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1107980 Share on other sites More sharing options...
monkeybidz Posted September 6, 2010 Author Share Posted September 6, 2010 Not getting any result still. Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1107988 Share on other sites More sharing options...
litebearer Posted September 6, 2010 Share Posted September 6, 2010 might try.... (modify to fit your params) $sql = mysql_query("SELECT id FROM table_name ORDER BY id DESC LIMIT 1"); $row = mysql_fetch_assoc($sql); $item_number = $row['id']; Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1107993 Share on other sites More sharing options...
kickstart Posted September 6, 2010 Share Posted September 6, 2010 Hi Why do you need this? If you need it to determine the last parent record id to insert related children then getting the max id is not a very safe way of doing it. Using mysql_insert_id. If you do just want the max number then either of the previous 2 suggestions should work (although I prefer systemicks suggestion), and if they don't then it suggest a different issue. I would put an or die(mysql_error()); on the query line to check for an error. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1107999 Share on other sites More sharing options...
monkeybidz Posted September 6, 2010 Author Share Posted September 6, 2010 I need it to display a total amount of items to date which in my case would be the last id count and not rows. I tried both others ways, but still get : Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108002 Share on other sites More sharing options...
kickstart Posted September 6, 2010 Share Posted September 6, 2010 Hi The id can easily not be the count (do an insert and roll it back and you will land up with a gap in the ids). You might be OK at the moment but it would be very easy for this to change in the future. Try this:- $result = mysql_query("SELECT MAX(idnum) as id from ready_aclassft") or die(mysql_error()); $row = mysql_fetch_row($result ); echo $row['id']; All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108004 Share on other sites More sharing options...
monkeybidz Posted September 6, 2010 Author Share Posted September 6, 2010 Will not echo $row['id'] Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108005 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2010 Share Posted September 6, 2010 mysql_fetch_row() won't return an associative array. You would need to use mysql_fetch_assoc() Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the errors php detects will be reported and displayed? You will save a ton of time. Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108007 Share on other sites More sharing options...
monkeybidz Posted September 6, 2010 Author Share Posted September 6, 2010 Wow, you did it. that was the problem. Thanks to all! Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108009 Share on other sites More sharing options...
vijdev Posted September 7, 2010 Share Posted September 7, 2010 this will work only if you are not going to delete any rows - depends on your application. am assuming your field is an aut-inc field that you are trying to play with.. suppose your last was 100, and then a transaction deleted 100, then you get answer as 99, based on the current methods suggested...which truly is not the ultimately correct answer..u still need to account for the 100th value.. now if you need this to predict truly the next auto-inc, use below: http://blog.jamiedoris.com/geek/560/ <? $tablename = "tablename"; $next_increment = 0; $qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'"; $qShowStatusResult = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "<br/>" . $qShowStatus ); $row = mysql_fetch_assoc($qShowStatusResult); $next_increment = $row['Auto_increment']; echo "next increment number: [$next_increment]"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108089 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2010 Share Posted September 7, 2010 You cannot 'get' the next available auto-increment value and use it, because between the point where you get it and the point where you use it, another query could be executed that uses that value and the value you 'got' is is no longer the next available value. Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108100 Share on other sites More sharing options...
vijdev Posted September 7, 2010 Share Posted September 7, 2010 You cannot 'get' the next available auto-increment value and use it, because between the point where you get it and the point where you use it, another query could be executed that uses that value and the value you 'got' is is no longer the next available value. looks like i've been mistaken... 1.then, the max method is also not correct, if you have 20 deleted items between 1->100, and the latest(101) also deleted, the max will say 100, but in reality you have only 80 2. what are the chances that your next auto-inc value gets overtaken in a few micro-seconds? Quote Link to comment https://forums.phpfreaks.com/topic/212701-last-id-number-ffrom-database/#findComment-1108104 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.