spuy767
spuy767

Reputation: 89

Return MySQL timestamp on insert

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

Answers (4)

Robert Co
Robert Co

Reputation: 1715

Use a multi-statement query.

  1. Select the current timestamp into a variable.
  2. insert the row with the timestamp.
  3. return (select) the variable to php.

Upvotes: 1

Rohan Kumar
Rohan Kumar

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

John Woo
John Woo

Reputation: 263683

As far as I know, it is possible for last inserted ID if the ID was set as AUTO_INCREMENTed. 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

John Conde
John Conde

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

Related Questions