Jump to content

selecting last record, is this mysql_query correct?


coder9

Recommended Posts

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
Share on other sites

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
Share on other sites

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.