php mysql_insert_id()

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 437

Post 3+ Months Ago

I've been using mysql_insert_id() for a few years now to get the auto-increment key from my last query. I guess I have two questions.
Code: [ Select ]

$sql = "INSERT INTO blah blah blah";
$run = mysql_query($sql);
$autokey = mysql_insert_id();
  1. $sql = "INSERT INTO blah blah blah";
  2. $run = mysql_query($sql);
  3. $autokey = mysql_insert_id();



Does it always get the last query executed going down the page? Php is read top to bottom right? But I thought there were instances that were different.

Can I chain the queries?

Code: [ Select ]

$sql = "INSERT INTO blah blah blah";
$run = mysql_query($sql);
$autokey = mysql_insert_id();

$sql2 = "INSERT INTO blah blah blah";
$run2 = mysql_query($sql2);
$autokey2 = mysql_insert_id();
  1. $sql = "INSERT INTO blah blah blah";
  2. $run = mysql_query($sql);
  3. $autokey = mysql_insert_id();
  4. $sql2 = "INSERT INTO blah blah blah";
  5. $run2 = mysql_query($sql2);
  6. $autokey2 = mysql_insert_id();




I guess I'm just concerned with the stability of retrieving that key. Also, if I have a lot of people accessing the file at one time will it effect the call, for instance, if a good deal of data is being written to the db in a short period?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

PHP goes straight down the page unless you change the flow with a loop of some sort.

As to the mysql_insert_id()

Quote:
Parameters

link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.


mysql_insert_id() will only return the id for the last insert/update query you ran with your mysql connection (link). Each time you call mysql_connect() a new link is made to the database.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 437

Post 3+ Months Ago

Great, that cleared up the flow and being that the parameter is link based then no two different links can get the same last id and so that clears up the stability issue that I'm concerned with.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 56 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.