如何将日期变量传递给php查询

时间:2018-02-19 16:19:03

标签: php mysqli

我一直在尝试许多不同的方法将日期作为变量传递给查询,而我尝试过的任何工作都没有。当我在下面输入确切的日期时,查询工作正常。

$ly = mysqli_query($link,'SELECT SUM(rtype_price) FROM client WHERE date BETWEEN "2017-01-01" AND "2017-12-31"');

while($row1 = mysqli_fetch_array($ly))
  {
    $ly_cash = $row1['SUM(rtype_price)'];
  }

例如,我一直在尝试这样的事情:

$ly_start = date('Y-m-d', strtotime('first day of previous year'));
$ly_end = date('Y-m-d', strtotime('last day of previous year'));

然后将$ ly_start和$ ly_end传递给查询,如下所示:

$ly = mysqli_query($link,'SELECT SUM(rtype_price) FROM client WHERE date BETWEEN "$ly_start" AND "$ly_end"');

1 个答案:

答案 0 :(得分:-1)

反转引号就行了。这有效:

$ly = mysqli_query($link,"SELECT SUM(rtype_price) FROM client WHERE paid = 1 AND date BETWEEN '$ly_start' AND '$ly_end'");
  while($row1 = mysqli_fetch_array($ly))
  {
    $ly_cash = $row1['SUM(rtype_price)'];
  }

谢谢ild_flue