简体   繁体   中英

PHP - Count SQL Results Per Day

So I have a table called Users which contains a table structure like this:

Lets say I have a mysql table called 'signups' with the following values:

UID|Name|Regdate
1|Admin|2014-03-04 10:51:01
2|Demo|2014-05-04 09:51:05

I want to create a graph showing how many people signed up in one day. I used this SQL Query:

SELECT DATE(regdate) AS `Date` , COUNT(*) AS Signups FROM `users` GROUP BY DATE(regdate)

It gave me an output of:

Date|Signups
2014-03-04|1
2014-05-04|1

I wanted the output in a PHP File so I made this

<?php include_once("inc/db.php"); ?>

<?php 
$query ="
SELECT  DATE(regdate) AS `Date`
    , COUNT(*) AS Signups 
FROM    `users` 
GROUP BY 
    DATE(regdate)
    ";
     $query_params = array( 
    ); 
     try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    {
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    echo $result;
    ?>

When I try to access the page, the result I get should be same thing I got from the SQL Query. However the result I get is 1 . As you can see, I am using PDO. I am a beginner please help me please. :)

Do you use mysqli or PDO ? Anyway execute does not do the job of returning the eventual result, assuming you use PDO , you have to:

 try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 

        foreach($stmt->fetchAll() as $fetch)
        {
            // do something
        }
    } 
    catch(PDOException $ex) 
    {
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    echo $result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM