?
Solved

Counting the number of records in an array

Posted on 2005-02-28
14
Medium Priority
?
442 Views
Last Modified: 2006-11-17
I am attempting to count the number of record in an array, im probably way off, but i am getting the following error.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/admin/public_html/babypool.php on line 78

$result = mysql_query("SELECT * FROM pool WHERE Sex=M");
$array = mysql_fetch_array($result);
echo count($array);
0
Comment
Question by:SuprSpy79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13422594
echo mysql_num_rows($result);
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13422598
instead of echo count($array);
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13422608
But according to your message, it also seems that you have an error in your query.

Batalf
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:ldbkutty
ID: 13422609
$result = mysql_query("SELECT * FROM pool WHERE Sex = 'M'") or die("Sql error : " . mysql_error());
if(mysql_num_rows($result) > 0) {
 $array = mysql_fetch_array($result);
 echo count($array);
}
else {
 echo "No records found.";
}
0
 
LVL 5

Author Comment

by:SuprSpy79
ID: 13422622
now im getting these two errors

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/admin/public_html/babypool.php on line 78
0
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/admin/public_html/babypool.php on line 80
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 13422626
Looks like there's an error in your query.  It should be ... WHERE Sex='M'
You can see these errors by always writing:
mysql_query("SELECT...") or die(mysql_error());
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13422631
Make sure that you are connected to the database server by

$db = mysql_connect("server","username","password");
mysql_select_db("databaseName",$db);

and make sure that this query:

SELECT * FROM pool WHERE Sex=M

is correct by running it directly in MySql.
0
 
LVL 20

Accepted Solution

by:
virmaior earned 100 total points
ID: 13422634
$result = mysql_query("SELECT * FROM pool WHERE Sex = 'M'") or die("Sql error : " . mysql_error());
if(mysql_num_rows($result) > 0) {
 while ($array[] = mysql_fetch_array($result))  {  }
 echo count($array);
}
else {
 echo "No records found.";
}

ldbkutty's will tell you how many fields in one record
Batalf's will tell you the total number of records retrieved
if you meant to get all the data first, then use this code.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13422639
>> Counting the number of records in an array
Only one record will be fetched in mysql_fetch_array.

mysql_num_rows($result) retrieves the number of records fot the query.
0
 
LVL 5

Author Comment

by:SuprSpy79
ID: 13422684
That was it virmaior - thanks - ldbkutty had the same answer correct?

I need to run this two one for M and one for F - basically to display the number of males and females - is there a better way to do this than running the code twice?
0
 
LVL 5

Author Comment

by:SuprSpy79
ID: 13422732
this works although im sure its not too efficient

$result = mysql_query("SELECT * FROM pool WHERE Sex = 'M'") or die("Sql error : " . mysql_error());
        if(mysql_num_rows($result) > 0) {
        while ($array[] = mysql_fetch_array($result))  {  }
        echo "<div align='center'><strong>Males:</strong> " . count($array) . "</div>" ;
}
$result2 = mysql_query("SELECT * FROM pool WHERE Sex = 'F'") or die("Sql error : " . mysql_error());
        if(mysql_num_rows($result2) > 0) {
        while ($array2[] = mysql_fetch_array($result2))  {  }
        echo "<div align='center'><strong>Females:</strong> " . count($array2) . "</div>" ;
}
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 13422733
SELECT SUM(IF(Sex = 'M', 1, 0)) AS numMales, SUM(IF(Sex='F', 1, 0)) AS numFemales FROM pool;
0
 
LVL 5

Author Comment

by:SuprSpy79
ID: 13422804
then i just echo numMales and numFemale?
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 100 total points
ID: 13423013
Yep.  Once you pull them out of the query results, of course.

$result = mysql_query(THE_QUERY_ABOVE) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['numMales'];
echo $row['numFemales'];
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question