Home > Code Snippets > Add data to a MySQL database
Add data to a MySQL database
Description
If you insert a new value to a MySQL database, and the id is an auto_increment, this simple function will obtain the id for you without having to do a new query on the database. This is the easiest way to get the id from a newly added row using a MySQL insert query without having to do a second query.
The code
<?php
// Do your insert query...
mysql_query("INSERT etc...");
// This finds the id of the row once it has been added...
$id = mysql_insert_id();
// Display it...
echo $id;
?>
Get the code
Download the file to your computer: Click here to get the file
