Problem is: I want to count the number in my column r_start
where the r_start
dates are between my other two variables. But it always gives me the Error Notice: Undefined variable: z in [path] on line 173
. So it doesn't fill the variable, what makes me think that the COUNT
does not work porperly. I did a lot of reasearch and tried some things, but I simply don't find the cause..
$sqld = "SELECT r_start, COUNT (*) FROM reservation WHERE r_start BETWEEN '".$cid."' and '".$cod."' GROUP BY r_start";
if ($result = $con->query($sqld)) {
$z = mysqli_fetch_assoc($result);
}
This error only appears with the COUNT
tag. In my other queries it works absolutly fine. f.e.:
$sqlc = "SELECT * FROM reservation where r_ende between '".$cid."' and '".$cod."'";
if ($result = $con->query($sqlc)) {
$y = mysqli_fetch_assoc($result);
}
Can anybody tell my why? Have I done something wrong?
MySQL generally does not recognize a space between a function and the opening paren. I would also advise that you give the column an alias:
SELECT r_start, COUNT(*) as cnt
FROM reservation r
WHERE r_start BETWEEN '".$cid."' and '".$cod."'
GROUP BY r_start;
You should also learn to use parameters to pass values into queries. Not using parameters makes the code subject to unexpected (and hard-to-debug) syntax errors, as well as making it vulnerable to SQL injection.