How can the NOW() function in MySQL be utilized to store the current date and time in a DATETIME column when working with PHP?
When working with PHP and MySQL, you can utilize the NOW() function in MySQL to store the current date and time in a DATETIME column. This can be achieved by simply including the NOW() function in the SQL query when inserting or updating records in the database.
<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Insert current date and time into a DATETIME column using NOW() function
$query = "INSERT INTO table_name (datetime_column) VALUES (NOW())";
mysqli_query($connection, $query);
// Close database connection
mysqli_close($connection);
?>
Related Questions
- How can conditional matching be implemented in PHP regular expressions to only match patterns if certain conditions are met, such as the absence of a specific character after a match?
- What are the advantages of using associative arrays in PHP compared to other data structures?
- In what ways can PHP functions be optimized to efficiently process and manipulate multiple images within a text string?