I study when I leave and collect code fragments when I leave and work on it for the last few days, and now I have thrown my towel in search of help.
I am trying to calculate the sum of two columns in my database using PDO.
here is my code
<?php
$host = "localhost";
$db_name = "dbname";
$username = "root";
$password = "root";
try {
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}
catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
$query = "SELECT SUM (fill_up) AS TotalFill,
SUM (mileage_covered) AS Totalmiles
FROM fuel_cost";
$row = $query->fetch(PDO::FETCH_ASSOC);
$total_fill = $row['TotalFill'];
$total_miles = $row['Totalmiles'];
$myanswer = $total_fill/$total_miles;
echo $myanswer
?>
I also checked the database table and both columns are varchar (32)
The error I get is that Call to a member function fetch() on a non-object
I was looking for this, but not sure if the problem with the above code is thanks in advance
source
share