Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: count question

  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts

    count question

    Hi i am counting total number of customers on first load so i can post the total. That is all this part does.

    so my query is

    PHP Code:

    //get total customers
    $query "SELECT COUNT(*) FROM users";
    $res =  mysqli_query($mycon,$query);
    $cntr mysqli_fetch_array($res); 

    When i do the print_r on $cntr it shows


    PHP Code:
    Array
    (
        [
    0] => 4
        
    [COUNT(*)] => 4


    I know i can do it this way as well.

    PHP Code:

    $query 
    "SELECT COUNT(*) AS tnum FROM users"

    then the result will be

    PHP Code:
    Array
    (
        [
    0] => 4
        
    [tnum] => 4

    my questions are;

    1. cant i just do it without the tnum option and just always display [0], either way it looks like [0] will always be a reliable correct value?

    2. i am curious why there are two elements, i never realized that it showed this way [COUNT(*)] => 4 and its kind of strange seeing that format. Im just curious why either way there are two elements a [0] and the other one.

    Thanks..

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There are two items since you have used fetch_array. Fetch array by default will always return MYSQLI_BOTH which is the same as MYSQLI_NUM | MYSQLI_ASSOC unless you have specified otherwise. Therefore you get both numerical offset and associative offset.
    fetch_row is reliable. It will always be 0 corresponding to the first property in the returned resultset record.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    durangod (02-16-2013)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you are intending to do a second query to actually retrieve data from all those rows then counting them first is unnecessary as you can easily obtain the count of the number of rows that are returned from a query without having to run a second query to get the count.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    durangod (02-16-2013)

  • #4
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,383
    Thanks
    264
    Thanked 32 Times in 31 Posts
    @Fou-Lu thanks how about that i never used mysqli_fetch_row before i have always relied on fetch_array or fetch_object.

    And it also looks like i dont even have to worry about extra elements on the result other than [0] because from what i gather from the docs it defaults to [0] and thats all i need as i am not repeating this in a loop so it will always be just [0] result on this. So just doing this will always get me the correct value.

    PHP Code:

    //get total customers 
    $query "SELECT COUNT(*) FROM users"
    $res =  mysqli_query($mycon,$query); 
    $cntr mysqli_fetch_row($res);  
    $totcust $cntr[0]; 
    But that does bring up a question, i have seen at times when someone in the past has used MYSQLI_NUM | MYSQLI_ASSOC for example.
    mysqli_fetch_array($resultval,MYSQLI_ASSOC)

    So should i be replacing those with the normal fetch of just ($resultval) as that second parameter is not required anymore i dont think.



    @felgall thanks, yes but i need to do this seperate because my other querys are in such a fashion that none of them pull all the rows, they are all limited by sorting vars and limiter status vars.
    Last edited by durangod; 02-16-2013 at 12:11 AM.

  • #5
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this,
    Select count(*) from Table where Employ Name="ABCD";
    i hope this will help you out

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,453
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Quote Originally Posted by Indor View Post
    Try this,
    Select count(*) from Table where Employ Name="ABCD";
    i hope this will help you out
    ** WRONG ANSWER **

    Aside from the fact that it is irrelevant to what he is asking, that code is illegal and will cause a SQL error.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by durangod View Post
    And it also looks like i dont even have to worry about extra elements on the result other than [0] because from what i gather from the docs it defaults to [0] and thats all i need as i am not repeating this in a loop so it will always be just [0] result on this.
    Yes - because your query is only retrieving a single value per row. If you were retrieving a second field from the table in the same query the it would be in [1].

    Even if you were returning multiple rows the count would be in [0] for every row when processing inside the loop.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #8
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Indor View Post
    Try this,
    Select count(*) from Table where Employ Name="ABCD";
    i hope this will help you out
    what worng in this???
    Last edited by Fou-Lu; 02-26-2013 at 11:20 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •