How can the current date and time be stored in a MySQL table using PHP?
To store the current date and time in a MySQL table using PHP, you can use the NOW() function in your SQL query. This function will automatically insert the current date and time when the query is executed. You can simply include this function in your INSERT or UPDATE query to store the current timestamp in a specific column of your MySQL table.
<?php
// Connect to MySQL 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 current date and time into MySQL table
$sql = "INSERT INTO table_name (column_name) VALUES (NOW())";
if ($conn->query($sql) === TRUE) {
echo "Record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close connection
$conn->close();
?>
Related Questions
- What are the advantages and disadvantages of implementing CAPTCHA in PHP forms to prevent spam submissions?
- What are the common pitfalls of not properly structuring PHP code within a while loop?
- Are there any best practices for improving the readability and efficiency of PHP scripts that retrieve data from a database and encrypt it?