PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

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


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:

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



More mysql PHP Questions

How do I insert the date into my table?
How do I limit the number of rows returned?
How do I include a conditional in my mysql query?
How do I order the results of my query?
How do I set a default value for ENUM fields in PHP?