Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2013, 10:56 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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..
durangod is offline   Reply With Quote
Old 02-15-2013, 11:26 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
durangod (02-16-2013)
Old 02-15-2013, 11:33 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
durangod (02-16-2013)
Old 02-16-2013, 12:07 AM   PM User | #4
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
@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..
durangod is offline   Reply With Quote
Old 02-22-2013, 12:59 PM   PM User | #5
Indor
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Indor is an unknown quantity at this point
Try this,
Select count(*) from Table where Employ Name="ABCD";
i hope this will help you out
Indor is offline   Reply With Quote
Old 02-22-2013, 06:29 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-22-2013, 07:11 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 02-25-2013, 03:52 PM   PM User | #8
Indor
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Indor is an unknown quantity at this point
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..
Indor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:16 AM.


Advertisement
Log in to turn off these ads.