Jump to content



Photo

[MySQL] How do I find the ID of a new row added to a table?


  • This topic is locked This topic is locked
No replies to this topic

#1 Cryo Kinesis

Cryo Kinesis

    Haphost Staff

  • Moderators
  • 128 posts
  • LocationGridania, Eorzea

Posted 05 August 2014 - 04:29 PM

Hi there,

 

If you are adding data to a mySQL table then you will often want to know the ID of a row you have just inserted so that you can reference it elsewhere. Often you’ll add to a table with an auto_increment ID field and then want to use this to store other information on other tables.

Here’s how you do it:

$q = mysql_query("INSERT INTO mytable VALUES('test')");
$rownumber = mysql_insert_id();
//$rownumber contains the id of the new row
?>

Cryo%20Kinesis%20Haphost%20Sig2.png





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users