Lukas Lukas - 11 months ago 83
MySQL Question

PDO mysql display with COUNT DISTINCT

I am trying to obtain the number of determinated task types, but it gives me error: Trying to get property of non-object

$statement = $pdo->prepare("SELECT COUNT(DISTINCT Company_Name)
FROM salesplus WHERE `Task_Type` = 'Form Sent'
AND `Consultant_name` = 'Name Surname'");


foreach($result as $row){
echo $row->Task_Type;

Thank you a lot!

Answer Source

That's not how it works... try (to get count of exact task):

SELECT COUNT(*) as count FROM (SELECT DISTINCT Company_Name FROM salesplus WHERE `Task_Type` = 'Form Sent' AND `Consultant_name` = 'Name Surname') as s

Or better (get list of companies and count of selected tasks for each company):

SELECT `Company_Name`,count(*) as count FROM salesplus WHERE `Task_Type` = 'Form Sent' AND `Consultant_name` = 'Name Surname' GROUP BY `Company_Name`