0

I've been trying many different ways to pass the dates into the query as a variable, and nothing I've tried has worked. When I put the exact dates in there like below, the query works fine.

$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)'];
  }

For example, I've been trying something like this:

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

then passing $ly_start and $ly_end into the query like this:

$ly = mysqli_query($link,'SELECT SUM(rtype_price) FROM client WHERE date BETWEEN "$ly_start" AND "$ly_end"');
  • Concatenation problem : `BETWEEN "'.$ly_start.'" AND "'.$ly_end.'"'` – Syscall Feb 19 '18 at 16:20
  • You will never get the value as expected if you put a variable inside a single quote. ` "$ly_start" AND "$ly_end"` will out put something like ` "$ly_start" AND "$ly_end"` not `YYYY-mm-dd AND YYYY-MM-DD` as expected, – ild flue Feb 19 '18 at 16:26
  • Wow, so simple. That was the trick, inverting the single/double quotes. Starting with "SELECT instead of 'SELECT and then the date variables as '$ly_start' worked. Thank you! – chefgoot Feb 19 '18 at 16:50
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Jo. Jun 28 '18 at 16:24

1 Answers1

-1

Inverting the quotes did the trick. This works:

$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)'];
  }

Thank you ild_flue