Reputation: 89
When I perform an insert statement to a MySQL database in PHP, is there a way to return the auto-generated timestamp or do I need a separate call to the database to retrieve the data?
Edit: I forgot that MySQL doesn't use epoch dates. I decided to just use an autoincrementing integer and a stored procedure.
Upvotes: 3
Views: 1381
Reputation: 1715
Use a multi-statement query.
Upvotes: 1
Reputation: 40639
You can return a last inserted id
after insert
, like:
<?php
$sql=mysql_query("INSERT INTO <TABLE> VALUES (?,?)");
return mysql_insert_id();
?>
Upvotes: 0
Reputation: 263683
As far as I know, it is possible for last inserted ID if the ID
was set as AUTO_INCREMENT
ed. But for timestamp, I'll advise you to create a stored procedure
so you will only have single request on the database. Remember that connection into database is costly.
Upvotes: 0
Reputation: 219794
You need to make a separate request to get that information. You can only get the last insert ID of auto incremented tables when doing an INSERT.
Upvotes: 2