I am trying to do a MYSQL LEFT JOIN query, however I'm getting an error: "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in". Because the query which is returned is false.
The code is:
$query = "
SELECT a.page
, SUM(b.views) views
, ROUND(SUM(b.costs)/1.38,2) costs
, ROUND(SUM(a.amount),2) REV
, ROUND(SUM(a.amount) - SUM(b.costs)/1.38,2) PNL
FROM revenues a
LEFT
JOIN costs b
ON a.page = b.page
WHERE a.date BETWEEN '$start_date' AND '$end_date'
AND a.country = '$country'
AND b.date BETWEEN '$start_date' AND '$end_date'
GROUP
BY a.page";
$result = mysqli_query($connection,$query);
if ($result != false) {
$foundnum = mysqli_num_rows($result);
if ($foundnum == 0) {
echo 'No results found.';
}
} else {
echo 'returned false';
}
printQuery($result);
printQuery is a function of me which creates a html table.
If I do two seperate queries on the tables I get correct tables back. My seperate queries are:
$query = "SELECT page, round(sum(amount),2) as REV FROM revenues WHERE date between '$start_date' AND '$end_date' AND country = '$country' GROUP BY page" ;
$query2 = "SELECT page, sum(views) as views, round(sum(costs)/1.38,2) as costs FROM costs WHERE date between '$start_date' AND '$end_date' GROUP BY page";
Can someone please point to me where the mistake is.
Table revenues consists of the columns: date: date amount: float (NULL if no revenue was made) country: varchar
Table costs cosists of the columns: date: date costs: float (always >= 0) views: int
Thanks
The following is usless:
else {
echo 'returned false';
}
Replace it with something like:
else {
echo "Query failed.
MySQLi error: " . mysqli_error();
}
You get points for at least checking your return values, but you need to make your code fail descriptively at least for dev purposes.