PHP count function question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
craigy101
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 12:50 pm

PHP count function question

Post by craigy101 »

Heya, I'm pretty new to PHP but have been getting on pretty well with it thus far, until now - it should be simple but I'm having a problem with the PHP Count function always returning the same value - 16 !

the code is here-
$query = "select * FROM articles WHERE keywords LIKE '$srch' || otherkeywords LIKE '$srch'";
$result = mysql_query($query);
$r=0;
$r=mysql_fetch_array($result);
echo count($r);

Now as you can see Im trying to echo the value of the array $r - but despite the number of returned results, its always printing out 16??
The code after this is a pretty standard WHILE condition that prints out the results. The results are exactly as expected, ranging from 1 to 30ish depending on the searched string, but the count value is always 16!!?!?! Its probably me being stupid but does anyone have any ideas ? This is the first time Ive used the Count function.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP count function question

Post by jackpf »

I think you want mysql_num_rows().

Count() will return the number of columns you have, since that's what mysql_fetch_array() does - returns the columns. And since you haven't specified any association, it'll be double the number of columns (with numerical and associated index keys).


:)
craigy101
Forum Newbie
Posts: 6
Joined: Thu Jul 30, 2009 12:50 pm

Re: PHP count function question

Post by craigy101 »

Ahh of course you are right, I had a feeling I was using the wrong function, thanks jack.

For anyone as silly as me, the final working code -
$query = "select * FROM articles WHERE keywords LIKE '$srch' || otherkeywords LIKE '$srch'";
$result = mysql_query($query);
$noarticles=mysql_num_rows($result);
echo $noarticles;

Cheers
Craig
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP count function question

Post by jackpf »

:)
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: PHP count function question

Post by straightman »

i dont think the problem is in the count but in the mysql_fetch_array where you need your second argument


======================================================================================







craigy101 wrote:Heya, I'm pretty new to PHP but have been getting on pretty well with it thus far, until now - it should be simple but I'm having a problem with the PHP Count function always returning the same value - 16 !

the code is here-
$query = "select * FROM articles WHERE keywords LIKE '$srch' || otherkeywords LIKE '$srch'";
$result = mysql_query($query);
$r=0;
$r=mysql_fetch_array($result);
echo count($r);

Now as you can see Im trying to echo the value of the array $r - but despite the number of returned results, its always printing out 16??
The code after this is a pretty standard WHILE condition that prints out the results. The results are exactly as expected, ranging from 1 to 30ish depending on the searched string, but the count value is always 16!!?!?! Its probably me being stupid but does anyone have any ideas ? This is the first time Ive used the Count function.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP count function question

Post by jackpf »

The second argument isn't required - it's just used to specify array index association.

Anyways, mysql_num_rows() was what he was looking for :)
straightman
Forum Commoner
Posts: 48
Joined: Sun Apr 19, 2009 5:20 am

Re: PHP count function question

Post by straightman »

alright (Jackpf correct me if I am wrong, as you see I ve discovered that debugging or trying to debug someone elses code and seeing others solutions is a great way to learn)

I find a bit weird that he passes the results of mysql_fetch_array to a variable $r, because as far as I know that is sort of a translator for php to obtain it from a sql query but I cannot visualize what would be passed to $r as to be able to count it. So, in other words passing that to $r is not correct (and if I am wrong I will learn from my mistake)


======================================================================================
straightman wrote:i dont think the problem is in the count but in the mysql_fetch_array where you need your second argument


======================================================================================







craigy101 wrote:Heya, I'm pretty new to PHP but have been getting on pretty well with it thus far, until now - it should be simple but I'm having a problem with the PHP Count function always returning the same value - 16 !

the code is here-
$query = "select * FROM articles WHERE keywords LIKE '$srch' || otherkeywords LIKE '$srch'";
$result = mysql_query($query);
$r=0;
$r=mysql_fetch_array($result);
echo count($r);

Now as you can see Im trying to echo the value of the array $r - but despite the number of returned results, its always printing out 16??
The code after this is a pretty standard WHILE condition that prints out the results. The results are exactly as expected, ranging from 1 to 30ish depending on the searched string, but the count value is always 16!!?!?! Its probably me being stupid but does anyone have any ideas ? This is the first time Ive used the Count function.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP count function question

Post by jackpf »

I'm glad you've taken on the challenge :) Yes, it's a great way to learn.

You are correct - using mysql_fetch_array() was wrong, mysql_num_rows() is the correct function for counting rows returned from a result set.
Post Reply