Page 1 of 1
PHP count function question
Posted: Thu Jul 30, 2009 12:55 pm
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.
Re: PHP count function question
Posted: Thu Jul 30, 2009 12:58 pm
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).

Re: PHP count function question
Posted: Thu Jul 30, 2009 1:11 pm
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
Re: PHP count function question
Posted: Thu Jul 30, 2009 2:00 pm
by jackpf
:)
Re: PHP count function question
Posted: Thu Jul 30, 2009 2:30 pm
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.
Re: PHP count function question
Posted: Thu Jul 30, 2009 2:32 pm
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

Re: PHP count function question
Posted: Thu Jul 30, 2009 2:57 pm
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.
Re: PHP count function question
Posted: Thu Jul 30, 2009 4:04 pm
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.