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?

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 ..

 

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.

 

 

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.