php 将 Mysqli bind_param 与日期和时间列一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/805828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Mysqli bind_param with date and time columns?
提问by Keith Lyall
How do you insert data into a MySQL date or time column using PHP mysqli and bind_param?
如何使用 PHP mysqli 和 bind_param 将数据插入 MySQL 日期或时间列?
回答by VolkerK
Like any other string
像任何其他字符串一样
$stmt = $mysqli->prepare('insert into foo (dt) values (?)');
$dt = '2009-04-30 10:09:00';
$stmt->bind_param('s', $dt);
$stmt->execute();
回答by Keith Lyall
Timestamps in PHP are integers (the number of seconds from the UNIX epoch). An alternative to the above is to use an integer type date/time parameter, and the MySQL functions FROM_UNIXTIMEand UNIX_TIMESTAMP
PHP 中的时间戳是整数(从 UNIX 时代开始的秒数)。上面的替代方法是使用整数类型的日期/时间参数,以及 MySQL 函数FROM_UNIXTIME和UNIX_TIMESTAMP
$stmt = $mysqli->prepare("INSERT INTO FOO (dateColumn) VALUES (FROM_UNIXTIME(?))");
$stmt->bind_param("i", $your_date_parameter);
$stmt->execute();
回答by Vincent
For the current date/time you can use the MySQL standard routine. You do not have to prepare that.
对于当前日期/时间,您可以使用 MySQL 标准例程。你不必准备。
$query = "INSERT INTO tablename ";
$query .= "VALUES(?,?,?,NOW()) ";
$preparedquery = $dbaselink->prepare($query);
$preparedquery->bind_param("iii",$val1,$val2,$val3);
回答by jcromeros1987
I used the date( ) function and this solved me the problem.
我使用了 date() 函数,这解决了我的问题。
$stmt = $mysqli->prepare("INSERT INTO FOO (dateColumn) VALUES ?");
// 6/10/2015 10:30:00
$datetime = date("Y-m-d H:i:s", mktime(10, 30, 0, 6, 10, 2015));
$stmt->bind_param("s", $datetime);
$stmt->execute();
回答by Peter
Use the mySQL function now() for timestamp fields.
将 mySQL 函数 now() 用于时间戳字段。
回答by Wijay Sharma
first set the date & time using date() function-
首先使用 date() 函数设置日期和时间-
$date=date("d/m/y");
$time=date("h:i:sa");
then pass it into the prepared statement, just like any other string variable-
然后将其传递到准备好的语句中,就像任何其他字符串变量一样-
$stmt = $mysqli->prepare("INSERT INTO FOO (dateColumn, timeColumn) VALUES (?,?)");
$stmt->bind_param("ss", $date , $time);
$stmt->execute();
回答by Computer User
My answer may be slightly off-topic. I had a problem in bind_param when passing date. The date was in (mm/dd/yyyy hh:mm:ss)format. MySQL was not taking it in (yyyy/mm/dd)format (which is the default format of date type in MySQL) in bind_param.
我的回答可能有点题外话。传递日期时,我在 bind_param 中遇到了问题。日期是(mm/dd/yyyy hh:mm:ss)格式。MySQL(yyyy/mm/dd)在bind_param中没有采用格式(这是MySQL中日期类型的默认格式)。
The following date formatting solved the problem:
以下日期格式解决了这个问题:
date('Y/m/d', strtotime($date_variable))
回答by Eahiya
$q = "insert into order_details (order_id, product_id, product_name, product_quantity, product_price, created_at, updated_at) values (?, ?, ?, ?, ?, NOW(), NOW())";
$q = "插入 order_details (order_id, product_id, product_name, product_quantity, product_price, created_at, updated_at) 值 (?, ?, ?, ?, ?, NOW(), NOW())";
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'iisid',$ord, $pid, $_SESSION['product_name'], $qty, $price);
mysqli_stmt_bind_param($stmt, 'iisid',$ord, $pid, $_SESSION['product_name'], $qty, $price);

