I am trying to fetch some data from database based on user entered month and year and table name.
From month and year I calculate from_date and to_date but query is not working if I put dates between $from_date and $todate.
$tableName = $_REQUEST['tableName'];
$month = $_REQUEST['monthName'];
$year = $_REQUEST['yearName'];
// echo json_encode($tableName);
$tableName = json_encode($tableName);
//echo $tableName;
$from_date = date('Y-m-d',strtotime($year."-".$month."-01"));
//echo json_encode($from_date);
//$to_date = date('Y-m-d',strtotime($year."-".$month."-01"));
$to_date = date('Y-m-t', strtotime($from_date));
//echo json_encode($to_date);
$conn = new PDO("sqlite:../../assets/rule_data.db");
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$sqlQuery = "select * from $tableName WHERE date >= '".$from_date."' AND date <= '".$to_date."' ";
//$sqlQuery = "SELECT * FROM $tableName WHERE v_cr_sysdate >= '".$from_date."' AND v_cr_sysdate <= '".$to_date."' ";
//$sqlQuery = "SELECT * FROM $tableName";
//$sqlQuery = "select * from $tableName WHERE date >= '".convert('$from_date','%d-%m-%y')."' AND date <= '".date($to_date)."' ";
$sqlQuery = "select * from $tableName WHERE date between '". date('Y-m-d', strtotime($from_date))."' ";
$sqlQuery .= " AND date <='". date('Y-m-d', strtotime($to_date))."' ";
$query = $conn->query($sqlQuery);
echo json_encode($query);
echo json_encode(["riskModules"=>$query->fetchAll(PDO::FETCH_ASSOC)]);
If I remove AND consition and keep only where date >= '$from_date' It will work but not with date range of from and to date.
Please help where I am wrong in giving AND query to where clasuse.