I am trying to pull data from SQL database which match with input year and month. Below is the code for SQL query
<?php
class SelectAMonthGet extends ConnectDB {
var $year;
var $month ;
function __construct($n){
$this->year = $_POST['year'];
$this->month = $_POST['AnalyzeEnterAreaMonth'];
}
function SelectAMonthGetData(){
$sql = "SELECT * FROM wp_myexpenses WHERE YEAR(date) = '$year' AND MONTH(date) = '$month' order by date,id";
$result = $this->connect()->query($sql);
$numRows = $result->num_rows;
if($numRows > 0) {
while ($row = $result->fetch_assoc()){
$data[] = $row;
}
return $data;
}
}
}
?>
A connectdb function contains my database connection and I am extending that function for this SQL query. But somehow, the code is not recognizing the $_POST variables $year and $month. Above code not giving any result.
I tried to change the SQL query to
$sql = "SELECT * FROM wp_myexpenses WHERE YEAR(date) = '2019' AND MONTH(date) = '1' order by date,id";
and the code works fine. Not sure why the code is not working when I mention the variable. Can someone guide me here?