How can I store a timestamp value in a MySQL database with the TIMESTAMP data type?
To store a timestamp value in a MySQL database with the TIMESTAMP data type, you can use the NOW() function in your SQL query to insert the current timestamp into the database. This function will automatically generate the current timestamp when the query is executed, simplifying the process of storing timestamp values in your database.
<?php
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert a timestamp value into the database
$sql = "INSERT INTO table_name (timestamp_column) VALUES (NOW())";
if ($conn->query($sql) === TRUE) {
echo "Timestamp value inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close the database connection
$conn->close();
?>
Related questions
-
Xdebug error_reporting var_dump
-
PHP sort order criteria
-
PHP 5 delete folder delete data