0

I have the following date data, which is used to display MySQL data parameters

$tgaw=date("Y-m-10");
$tgak=date("Y-m-09", strtotime("+1 months"));

and I have the following query:

$querylembur = "SELECT SUM(total) as subtotal FROM lembur WHERE tgl between ".$tgaw." 
AND ".$tgak." AND staff =".$row['nama'];
$rslembur=mysqli_query($con,$querylembur);
$rowlembur=mysqli_fetch_array($rslembur);

The result I want is that the program will display data from date :$tgaw to date: $tgak

Amanjot Kaur
  • 2,028
  • 4
  • 18
  • 33
  • You need to enclose your literals in the query in quotes i.e. `$querylembur = "SELECT SUM(total) as subtotal FROM lembur WHERE tgl between '$tgaw' AND '$tgak' AND staff = '{$row['nama']}';` but better yet you should use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) – Nick Jan 24 '20 at 04:10

1 Answers1

1

You should be using prepared statements here, and also use the MySQL functions to add one month to the upper date in the range.

$sql = "SELECT SUM(total) AS subtotal FROM lembur WHERE tgl BETWEEN ? AND DATE_ADD(?, INTERVAL 1 MONTH) AND staff = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("sss", $tgaw, $tgaw, $row['nama']);
$stmt->execute();
// fetch here
$stmt->close();
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360