I'm trying to fetch all rows where the date column's value is a day in july using the code below:
$july="07";
$query=$conn->prepare("SELECT * FROM table WHERE date BETWEEN '2018-?-01' AND '2018-?-31'");
$query->execute($july,$july);
$row=$query->setFetchMode();
I have also tried like this:
$july="07";
$month_1="'2018-".$july."-01'";
$month_2="'2018-".$july."-31'";
$query=$conn->prepare("SELECT * FROM table WHERE date BETWEEN ? AND ?");
$query->execute(array($month_1,$month_2));
$row=$query->setFetchMode();
1st case, I get the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in file.php on line 35
I assume I get that error because I cannot have '?' in a query between single brackets.
Second case, I get nothing at all.
Solved it with this:
SELECT * FROM table WHERE YEAR(date)=2018 AND MONTH(date)=?