coder9 Posted July 26, 2008 Share Posted July 26, 2008 I'm not sure if this syntax correct. selecting the very last record of the table. is this correct? $query = "SELECT * FROM jwinners WHERE id='".mysql_insert_id()."'"; Thank you. Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/ Share on other sites More sharing options...
genericnumber1 Posted July 26, 2008 Share Posted July 26, 2008 I'd use SELECT * FROM jwinners ORDER BY id DESC LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599945 Share on other sites More sharing options...
dannyb785 Posted July 26, 2008 Share Posted July 26, 2008 I'd use SELECT * FROM jwinners ORDER BY id DESC LIMIT 1 No sense in doing a query if you don't have to. You do: $id = mysql_insert_id(); the function returns the primary key of the row that was most recently inserted. Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599949 Share on other sites More sharing options...
TempleDMDKrazd Posted July 26, 2008 Share Posted July 26, 2008 I'd use SELECT * FROM jwinners ORDER BY id DESC LIMIT 1 No sense in doing a query if you don't have to. You do: $id = mysql_insert_id(); the function returns the primary key of the row that was most recently inserted. is mysql_insert_id() used only if the primary key is auto_increment? Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599952 Share on other sites More sharing options...
JasonLewis Posted July 26, 2008 Share Posted July 26, 2008 Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries. Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599956 Share on other sites More sharing options...
samshel Posted July 26, 2008 Share Posted July 26, 2008 Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value. mysql_insert_id() can be called if you have fired an insert query before calling this in the same page..so if you are calling this function before inserting records, it will not work.. i suggest to use the method given by genericnumber1 .. Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599957 Share on other sites More sharing options...
JasonLewis Posted July 26, 2008 Share Posted July 26, 2008 See we both quoted from php.net, remember... It's pretty easy to type www.php.net/mysql_insert_id into your browser and you'll be greeted with a bucket load of information about the function. Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599958 Share on other sites More sharing options...
dannyb785 Posted July 26, 2008 Share Posted July 26, 2008 i suggest to use the method given by genericnumber1 .. not the best method. The only time you even need the id is right after inserting something. And if you insert multiple rows in one loop, just use the function and add the id's into an array. No sense using more queries than you need to. Queries aren't just dispensible as if you can use as many as you need with no fear of consequence. Most hosts limit the number of connections/queries allowed. Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599971 Share on other sites More sharing options...
dannyb785 Posted July 26, 2008 Share Posted July 26, 2008 See we both quoted from php.net, remember... It's pretty easy to type www.php.net/mysql_insert_id into your browser and you'll be greeted with a bucket load of information about the function. agreed! I know if you google "php" and then the name of the function you want, you will always get the php function as your first hit Link to comment https://forums.phpfreaks.com/topic/116684-selecting-last-record-is-this-mysql_query-correct/#findComment-599972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.