Home  >  Article  >  Backend Development  >  How to Insert Date and Time Data into MySQL using PHP mysqli and bind_param?

How to Insert Date and Time Data into MySQL using PHP mysqli and bind_param?

Barbara Streisand
Barbara StreisandOriginal
2024-10-27 04:07:02202browse

How to Insert Date and Time Data into MySQL using PHP mysqli and bind_param?

Binding Date and Time Columns with mysqli

Inserting date and time data into MySQL using PHP mysqli and bind_param requires treating them the same way as any other string. Here's how to do it:

<code class="php">$stmt = $mysqli->prepare('insert into foo (dt) values (?)');
$dt = '2009-04-30 10:09:00';
$stmt->bind_param('s', $dt);
$stmt->execute();</code>

In this code:

  • $stmt is a prepared statement object.
  • $dt is the date and time value to be inserted.
  • bind_param('s', $dt) binds the $dt variable to the first placeholder in the prepared statement. The 's' indicates that the value is a string.
  • execute() executes the prepared statement, inserting the $dt value into the dt column of the foo table.

The above is the detailed content of How to Insert Date and Time Data into MySQL using PHP mysqli and bind_param?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn