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

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 06-08-2005, 02:54 AM   PM User | #1
dragon
New Coder

 
dragon's Avatar
 
Join Date: May 2003
Location: Florida
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
dragon is an unknown quantity at this point
count function doubling

I'm not sure why this is working this way, but for some reason, when I use the PHP count function on an array that just came from, the number is doubling.

Here's the code where the problem is apparent:
PHP Code:
$query 'SELECT ID,name FROM users';
$result mysql_query($query);
$user mysql_fetch_array($result); 
$c count($user); 
echo(
$c); 
At this point, $c should be 2, but for some reason, it's showing 4!

This is causing problems when I'm trying to use a for loop with $c as the limit. I'm running into 'Notice: Undefined offset' when I use those.

I hope someone here knows what is causing this. Am I missing something or is my computer screwed up?
__________________
http://www.dragonshobbies.com
dragon is offline   Reply With Quote
Old 06-08-2005, 02:57 AM   PM User | #2
gsoft
Regular Coder

 
Join Date: Apr 2004
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
gsoft is an unknown quantity at this point
Maybe print_r your $user to check out why, also try using sizeof
gsoft is offline   Reply With Quote
Old 06-08-2005, 03:18 AM   PM User | #3
SeeIT Solutions
Regular Coder

 
Join Date: May 2005
Posts: 563
Thanks: 0
Thanked 3 Times in 3 Posts
SeeIT Solutions is on a distinguished road
I believe it is because you have 2 rows and 2 colums.

you could divide the result of count by the number of columns you have selected from the database.

it is counting all values in the array not rows.

The best solution however would be to use

PHP Code:
$query 'SELECT ID,name FROM users'
$result mysql_query($query);
$c mysql_num_rows($result);
echo 
$c
__________________
Design Portfolio
SeeIT Solutions is offline   Reply With Quote
Old 06-08-2005, 03:19 AM   PM User | #4
dragon
New Coder

 
dragon's Avatar
 
Join Date: May 2003
Location: Florida
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
dragon is an unknown quantity at this point
Ok. sizeof() gave the same as count(). print_r gives me this:

Array ( [0] => 1 [ID] => 1 [1] => Frank [name] => Frank )

I understand a little more... but is that normal to get the array with both numbers and names?

By the way, mysql_num_rows() gives 4 also.
__________________
http://www.dragonshobbies.com

Last edited by dragon; 06-08-2005 at 03:27 AM..
dragon is offline   Reply With Quote
Old 06-08-2005, 03:32 AM   PM User | #5
Kid Charming
Regular Coder

 
Join Date: Jun 2005
Posts: 804
Thanks: 0
Thanked 0 Times in 0 Posts
Kid Charming is an unknown quantity at this point
That's correct: mysql_fetch_array() retrieves results as both an associative and numerical array. If you only want an array of one type or the other, use mysql_fetch_row() for numeric indices or mysql_fetch_assoc() for associative. There's also another parameter you can pass to mysql_fetch_array() that'll limit it to one or the other, but I can't remember the specific syntax off the top of my head (I never use it).
Kid Charming is offline   Reply With Quote
Old 06-08-2005, 04:11 AM   PM User | #6
gsoft
Regular Coder

 
Join Date: Apr 2004
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
gsoft is an unknown quantity at this point
From the PHP Manual

Quote:
The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default value of MYSQL_BOTH.
E.g.

PHP Code:
$user mysql_fetch_array($resultMYSQL_ASSOC); 
gsoft is offline   Reply With Quote
Old 06-09-2005, 01:09 AM   PM User | #7
dragon
New Coder

 
dragon's Avatar
 
Join Date: May 2003
Location: Florida
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
dragon is an unknown quantity at this point
Thanks guys! It all makes sense now.
__________________
http://www.dragonshobbies.com
dragon is offline   Reply With Quote
Old 06-09-2005, 02:20 AM   PM User | #8
ClubCosmic
Regular Coder

 
Join Date: May 2004
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
ClubCosmic is an unknown quantity at this point
For my own curiosity will this work?

$query = 'SELECT count(*), ID,name FROM users';

tnx for any insight.
c.c.
ClubCosmic is offline   Reply With Quote
Old 06-09-2005, 03:51 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 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
Assuming that ID linked with the name:
Code:
$query = "SELECT COUNT(ID) AS count, ID, name FROM users";
Now your result will contain a count element for it. * should be fine as well, but the mysql gurus will probably tell you about a load time differential on that
Fou-Lu is offline   Reply With Quote
Old 06-09-2005, 04:11 AM   PM User | #10
Kid Charming
Regular Coder

 
Join Date: Jun 2005
Posts: 804
Thanks: 0
Thanked 0 Times in 0 Posts
Kid Charming is an unknown quantity at this point
Quote:
Originally Posted by ClubCosmic
For my own curiosity will this work?

$query = 'SELECT count(*), ID,name FROM users';
Not quite -- you can't mix aggregate functions with non-aggregate functions without having a GROUP BY clause. So you need something like

Code:
SELECT
 count(*) as mycount
,ID
,name
FROM
 table
GROUP BY
 ID
This comes with another gotcha, though. The above will give you an error in any SQL except MySQL, because we're selecting two non-aggregate fields (ID and name), but only grouping on one. But just because you're not getting an error in MySQL doesn't mean it's a good idea. For this query, there shouldn't be any problems, since ID'll be a unique value -- there will only be one name per ID, and the grouping is only a formality (this also means, of course, that your COUNT() value will always be 1 ). If your fields aren't unique, though, you can get some pretty skewed results. So the standard-friendly query is

Code:
SELECT
 COUNT(*) AS mycount
,ID
,name
FROM
 table
GROUP BY
 ID,
 name
As for efficiency and COUNT(), for something like this, COUNT(1) is best, since you don't have to pull anything out of the db itself.
Kid Charming 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 05:51 PM.


Advertisement
Log in to turn off these ads.