我有这个问题:
$stmt = $mysqli->prepare("
SELECT id, titulo
FROM posts WHERE
data < DATE_SUB( NOW( ) , INTERVAL 140 MINUTE )
AND
CURDATE() = DATE(data)
ORDER BY RAND( ) limit 1
");
$stmt->execute();
而不是now()我想使用PHP日期时间,任何想法如何使用它?
答案 0 :(得分:1)
是的,您可以将PHP变量作为绑定参数注入SQL语句:
$stmt = $mysqli->prepare("
SELECT id, titulo
FROM posts WHERE
data < DATE_SUB( ? , INTERVAL 140 MINUTE )
AND DATE(data) = DATE(?)
ORDER BY RAND( ) LIMIT 1");
$curdatetime = date("Y-m-d H:i:s");
$stmt->bind_param("ss", $curdatetime, $curdatetime);
$stmt->execute();