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.
PHP count function question
Moderator: General Moderators
Re: PHP count function question
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).

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
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
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
-
straightman
- Forum Commoner
- Posts: 48
- Joined: Sun Apr 19, 2009 5:20 am
Re: PHP count function question
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
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
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
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)
======================================================================================
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
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.
You are correct - using mysql_fetch_array() was wrong, mysql_num_rows() is the correct function for counting rows returned from a result set.