LeviZoesch
LeviZoesch

Reputation: 1621

PHP/SQL Sum total customer count

My database has duplicates for different reasons, and I am trying to get a total of unique customers per a users ID.

This is the current query that I have constructed.

    $ApprovedCustomerCount ="SELECT `".Customer_Full_Name."`, `".Customer_Address."`, COUNT(DISTINCT(`".Customer_Full_Name."`)) AS CustomersCount 
                            FROM `".OrdersTable."` 
                            WHERE `".Users_Sales_ID."`=$EMPID
                            AND `".Current_Order_Stage."`='".Current_Order_Stage_Approved."' 
                            GROUP BY `".Customer_Full_Name."`, `".Customer_Address."` 
                            HAVING COUNT(".Customer_Full_Name.") > 1";

    $ApprovedCustomerResult=mysql_query($ApprovedCustomerCount);
    while($OrdersRow=mysql_fetch_array($ApprovedCustomerResult))
    $CustomerApprovedCount = $OrdersRow['CustomersCount'];

    if (empty($CustomerApprovedCount)) { $CustomerApprovedCount = '0'; }    
echo $CustomerApprovedCount;

Mistakenly, it only gives me a value of 1 when I echo in PHP.

However, when I query the DB Table with the exact query I get an output ( for each customer name, address that are unique counts = 1) that will display the list of customers, and give me db row count of 67. That is the number I need.

Query in PhpMyAdmin I run

SELECT `CustomerName`, `CustomerAddress`, COUNT(DISTINCT(`CustomerName`)) AS Customers 
FROM `wrightway_orders` 
WHERE `Employee_ID` = '3020'
AND `Order Stage`='Approved'
GROUP BY `CustomerName`, `CustomerAddress` 
HAVING COUNT(*) > 1 

Outputs

+------------------+----------------------+----------------+
|  Customer Name   |  Address             | CustomersCount |
+------------------+----------------------+----------------+
| ADRIANE JOHNSON  |  10015 161ST PL NE   |        1       |
+------------------+----------------------+----------------+
| BILL SMITH       |  9923 161ST AVE NE   |        1       |
+------------------+----------------------+----------------+
| BRIAN WALTERS    |  11129 106TH AVE NE  |        1       |
+------------------+----------------------+----------------+

etc

I need to sum the total amount of Count defined as 'CustomerCount' values to then echo that value as echo $CustomerApprovedCount;

Where am I going wrong?

I appreciate your help :)

I need to count all customers that are unique, then give a sum total of all customers where Employee_ID=ID#.

Upvotes: 1

Views: 460

Answers (1)

Inpego
Inpego

Reputation: 2667

Just use mysql_num_rows with result:

$ApprovedCustomerResult = mysql_query($ApprovedCustomerCount);
$CustomerApprovedCount = mysql_num_rows($ApprovedCustomerResult);

Upvotes: 2

Related Questions