I'm writing this PHP program to query the database for some values and display it. The problem is if I execute it, an empty page is shown. I tried var_dump
on the result, it gives bool(false)
But this query works perfect on standalone SQL.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
//Access local db with this script
$servername = "localhost";
$username = "dev";
$password = "somepassword";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "with hours as (
select 0 hr
union all select hr + 1 where h < 23
);
select h.hr, count(e.eventID) as cnt
from hours h
left join events e
on e.timeStamp > now() - interval 1 day
and hour(e.timestamp) = h.hr
group by h.hr;";
$result = $conn->query($sql);
echo "";
if ($result) {
while ($obj = $result -> fetch_object()) {
printf("%s, ", $obj->cnt);
echo mysqli_error($this->conn) ;
}
$result -> free_result();
}
else
{
mysqli_error($conn);
}
echo "";
$conn->close();
Where am I going wrong?