0

我有 $year 和 $month,我需要选择时间戳在一年/月之后和另一年/月之前的所有记录

WHERE m.id = :magazineId
      AND t.isChargedBack IS FALSE
      AND t.`timestamp` >= STR_TO_DATE(:year'-08-01', '%Y-%m-%d')
      AND t.`timestamp` < STR_TO_DATE('2013-09-01', '%Y-%m-%d')

刚开始,我只删除了一个年份条目,查询停止查找任何行。

应该如何参数化它?

4

4 回答 4

0

本质上(为了示例而没有 PDO 参数)

PHP

$target = $year . '/' . $month . '/01';

SQL

" ...
WHERE m.id = :magazineId
AND t.isChargedBack IS FALSE
AND t.`timestamp` >= '$target'
AND t.`timestamp` < DATEADD('$target' + 1 MONTH);"
于 2013-08-23T21:36:02.457 回答
0

您是否尝试过使用:

`t`.`timestamp` BETWEEN "$DATE1" AND "$DATE2"?

MySQL 之间

您可能还想尝试而不是使用 STR_TO_DATE,将您的日期转换为时间戳

于 2013-08-23T20:51:29.077 回答
0
$mysqli = new mysqli("host", "user", "password", "world");
$mymagazineid = "Foo";
$year = "2013";

$parameter_clause = <<<EOD
   SELECT * FROM YourTable
   WHERE m.id = ?
   AND t.isChargedBack IS FALSE
   AND t.`timestamp` >= STR_TO_DATE(?, '%Y-%m-%d')
   AND t.`timestamp` < STR_TO_DATE('2013-09-01', '%Y-%m-%d')
EOD;

$stmt = mysqli_prepare($mysqli, $parameter_clause);
mysqli_stmt_bind_param($stmt, "ss", $mymagazineid, "$year-08-01");
mysqli_stmt_execute($stmt);
于 2013-08-23T20:54:33.553 回答
0

尝试这个:

$date1 = strtotime('01-08-$year');
$date2 = strtotime('01-09-2013');

$query = "SELECT * from your_table WHERE m.id = '{$magazineId}' AND t.isChargedBack IS FALSE
  AND t.`timestamp` BETWEEN  '{$date1}' AND '{$date2}'";
于 2013-08-23T21:01:40.103 回答