How to count a value with PHP

Discussion in 'PHP' started by extraspecial, Oct 11, 2012.

  1. #1
    Hello guys,

    I have datas in my database and for example there is a city column and I need to count how many people are from New York and show it on my dashboard table. I need to count the number of people form the city and show it in my dashboard table with $newyork for example. By the way I will do it for 80 cities so let me know if there is a way without repeting the steps for each city.

    Can someone help me with it assuming my database is database and table is table?

    Thanks
     
    extraspecial, Oct 11, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    not really should how your db is set up but if you use ony one table then the query below should work.

    
    SELECT *,COUNT(*) AS `tot` FROM `table` GROUP BY `city`
    
    Code (markup):
     
    plussy, Oct 11, 2012 IP
  3. extraspecial

    extraspecial Member

    Messages:
    788
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    45
    #3
    I don't thing my db would matter, its just about one table and one column, by the way I need to specifically count New York city and using PHP code.

     
    extraspecial, Oct 11, 2012 IP
  4. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #4
    It does matter. because without seeing what you actually have done so far, you leave us guessing.
    SELECT COUNT(*) AS `tot` FROM `table` WHERE `city`="New York City"
    Code (markup):
     
    plussy, Oct 11, 2012 IP
  5. extraspecial

    extraspecial Member

    Messages:
    788
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    45
    #5
    OK! How will it be in PHP?

    I used this but I only receive "1" as result...

    mysql_connect("localhost", "user", "pass") or die ("Problem with Connection...");
    mysql_select_db("database");
    $query = mysql_query("SELECT COUNT(*) AS tot FROM users WHERE city='New York'");
    $newyork = mysql_num_rows($query);

    Do you have any idea?

     
    extraspecial, Oct 11, 2012 IP
  6. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #6
    ok 2 things.


    1. the usage of the mysql extension is discouraged which means it shouldn't be used any more.

    2. this is how you need to do it.

    
    mysql_connect("localhost", "user", "pass") or die ("Problem with Connection...");
    mysql_select_db("database");
    $query = mysql_query("SELECT COUNT(*) AS tot FROM users WHERE city='New York'");
    $result = mysql_fetch_assoc($query);
    $newyork = $result['tot'];
    
    PHP:
    or if you are using the PDO library

    
    
    $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    
    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
    
    $sql = 'SELECT COUNT(*) AS `tot` FROM `users` WHERE `city`="New York"';
    $sth = $dbh->prepare($sql);
    $sth->execute();
    $result = $sth->fetch(PDO::FETCH_ASSOC);
    
    PHP:
    Yes it looks like a lot more code but trust me it is a lot safer to use it.
     
    plussy, Oct 11, 2012 IP
  7. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #7
    No problem. I like sharing my dote.
     
    plussy, Oct 11, 2012 IP
  8. Red Swordfish Media

    Red Swordfish Media Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can also try using the global mysql_num_rows function. This returns the number of rows where the query pulled the desired data. This way you never have to use COUNT at all.

    $data = mysql_query("SELECT * FROM users WHERE city = 'New York'");
    $num_rows3 = mysql_num_rows($data4);
    echo $num_rows3;
     
    Red Swordfish Media, Oct 11, 2012 IP
  9. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #9
    You are right but there are 2 problems.

    1. As I stated above using the mysql functions is DISCOURAGED. Just to make sure you understand what it means: It means you shouldn't use it! You should use an alternative.

    2. Your SQL is very bad. Why do you select first everything and store it in memory and then count it if you can let the database do the counting and store simply a single value in memory. This might not be a problem if you have a small table but if you have thousands of lines or even more then your SQL will actually slow everything down.

    Just because you get the same result doesn't mean that it's ok to use it.
     
    plussy, Oct 12, 2012 IP
  10. extraspecial

    extraspecial Member

    Messages:
    788
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    45
    #10
    Guys thank you for answers! I have solved it last night in this way;

    Is selecting one column better? What do you think?

    $query = mysql_query("SELECT COUNT(city) FROM users WHERE city='New York'");
    $newyork = mysql_result($query, 0);
     
    extraspecial, Oct 12, 2012 IP
  11. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #11
    Why do you use the mysql functions?? It is DISCOURAGED to use them. Don't you care or are you just lazy?
     
    plussy, Oct 12, 2012 IP
  12. extraspecial

    extraspecial Member

    Messages:
    788
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    45
    #12
    I really have no idea, its something I found on web as a solution and it worked...

    What's the problem to use them?

    Thanks

     
    extraspecial, Oct 13, 2012 IP
  13. Red Swordfish Media

    Red Swordfish Media Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    He is making the case that this is the old fashioned way of doing this. Nowadays many developers are using the PDO object instead of the mysql globals. It may be more secure to do it this way. But, if you haven't started using the more object-oriented practices yet then your solution will probably fit your needs. If you are doing a lot of PHP development then you might want to pick a some of the newer PHP books such as the newest book from sitepoint about PHP development. It will show you how to use the PDO object.
     
    Red Swordfish Media, Oct 13, 2012 IP
  14. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #14
    Another reason to not use the mysql functions is that they may be discontinued in the future. Then, when your host upgrades to a new version of PHP, your site stops working. And fixing it will be a nightmare, because you'll post your code, which is flawless, tell us it used to work but stopped, and we won't have read the email you got telling you that your host is now running MySQL 9.3 or something.
     
    Rukbat, Oct 14, 2012 IP
  15. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #15
    I would not recommend COUNT(city), instead do COUNT(*), which is much faster.

    Additionally, do not have the 0 in there unless you have multiple DB connections, in which you should have the Db resource connection instead.

    
    <?php
    $query = mysql_query("SELECT COUNT(*) FROM users WHERE city='New York'");
    $newyork = mysql_result($query); 
    
    PHP:
     
    ThePHPMaster, Oct 15, 2012 IP
  16. Drent123

    Drent123 Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    We used count method for counting the any value.
     
    Drent123, Oct 15, 2012 IP