ted_chou12 Posted January 8, 2007 Share Posted January 8, 2007 If I have an mysql column called id, and it contains only intergers, how do I echo the greatest id number?For example:ID12345Then I want to echo 5.Ted Link to comment https://forums.phpfreaks.com/topic/33319-solved-echoing-the-greatest-number/ Share on other sites More sharing options...
matto Posted January 8, 2007 Share Posted January 8, 2007 There are 2 ways you could do this:If the id field is an auto_increment you can use mysql_last_insert function or you can use the following in your sql statementselect max(id) as id from table... Link to comment https://forums.phpfreaks.com/topic/33319-solved-echoing-the-greatest-number/#findComment-155686 Share on other sites More sharing options...
ted_chou12 Posted January 8, 2007 Author Share Posted January 8, 2007 oh thanks, you are right, my id column is an auto_increment type, but how would I use the mysql_last_insert function ?ThanksTed Link to comment https://forums.phpfreaks.com/topic/33319-solved-echoing-the-greatest-number/#findComment-155687 Share on other sites More sharing options...
matto Posted January 8, 2007 Share Posted January 8, 2007 [code]<?php$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');if (!$link) { die('Could not connect: ' . mysql_error());}mysql_select_db('mydb');mysql_query("INSERT INTO mytable (product) values ('kossu')");printf("Last inserted record has id %d\n", mysql_insert_id());?>[/code]The mysql_insert_id() acts on the last performed query so if you are not doing an insert you are probably best to use something like the following:[code]<?php$sql = "select max(id) as max_id from table";$result = @mysql_query($sql) or die("<tt>problem with $sql</tt>");if($result) { $last_id = @mysql_fetch_object($result); echo $last_id->max_id;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/33319-solved-echoing-the-greatest-number/#findComment-155702 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 [quote]how would I use the mysql_last_insert function ?[/quote]Read the [url=http://php.net/mysql_last_insert]manual[/url]! Link to comment https://forums.phpfreaks.com/topic/33319-solved-echoing-the-greatest-number/#findComment-155703 Share on other sites More sharing options...
ted_chou12 Posted January 8, 2007 Author Share Posted January 8, 2007 thorpe, if you checked that link it says this:Sorry, but the function mysql_last_insert is not in the online manual. Perhaps you misspelled it, or it is a relatively new function that hasn't made it into the online documentation yet. The following are the 20 functions which seem to be closest in spelling to mysql_last_insert (really good matches are in bold). Perhaps you were looking for one of these: mysql_insert_idmysql_list_dbsmysql_list_fieldsmysqli_insert_idmysqli_stmt_initmysqli_stmt_resetsdo_list_insert sqlite_last_insert_rowidmsql_list_dbsmsql_list_fieldsmysql_change_usermysql_data_seekmysql_list_processesmysql_list_tables mysql_statmysqli_stmt_bind_resultmysqli_stmt_closemysqli_stmt_errorpdo_lastinsertidsqlite_last_error of which I already knew before I asked the question.Thanks matto, by the way.Ted Link to comment https://forums.phpfreaks.com/topic/33319-solved-echoing-the-greatest-number/#findComment-155908 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.