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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 .. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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.