WebRef.eu  - Internet Marketing and Online Business Resources  

Home / Site Map | Contact

 

Get Id of last record inserted into a MySQL database

If you are inserting records into a MySQL database, e.g. a number of consumer products, and are using a ProductId field which you set to auto increment to automatically assign a ProductId, you will often need to know what Id was assigned to the last record you added. MySQL has a function to do this, it's:

mysql_insert_id()

So, if you have for example performed the following MySQL insert statement:

//insert into database
$query = "INSERT INTO Products (ProductId, ProductName, ProductBrand, ProductUrl, ProductDesc, ProductAdminRating, ProductApproved, ProductDateAdded, SiteId) VALUES ('" .
makeSQLSafe($ProductId) . "', '" .
makeSQLSafe($ProductName) . "', '" .
makeSQLSafe($ProductBrand) . "', '" .
makeSQLSafe($ProductUrl) . "', '" .
makeSQLSafe($ProductDesc) . "', '" .
makeSQLSafe($ReviewRating) . "', '" .
makeSQLSafe($ProductApproved) . "', '" .
makeSQLSafe($DateAndTime) . "', '" .
makeSQLSafe($SiteId) . "')";

If you left the database connection open and then used the following line in your script:

echo mysql_insert_id() ;

This would display the Id (i.e. ProductId) of the last product you added.

 

 

 




Low Prices UK Shopping

Compare Prices
at LowPrices.co.uk


Home / Site Map | Contact

All Content ©2020 WebRef.eu