• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Counting the number of records in an array

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
SuprSpy79
Asked:
SuprSpy79
  • 4
  • 4
  • 3
  • +2
2 Solutions
 
BatalfCommented:
echo mysql_num_rows($result);
0
 
BatalfCommented:
instead of echo count($array);
0
 
BatalfCommented:
But according to your message, it also seems that you have an error in your query.

Batalf
0
Technology Partners: 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!

 
ldbkuttyCommented:
$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
 
SuprSpy79Author Commented:
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
 
snoyes_jwCommented:
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
 
BatalfCommented:
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
 
virmaiorCommented:
$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
 
ldbkuttyCommented:
>> 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
 
SuprSpy79Author Commented:
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
 
SuprSpy79Author Commented:
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
 
snoyes_jwCommented:
SELECT SUM(IF(Sex = 'M', 1, 0)) AS numMales, SUM(IF(Sex='F', 1, 0)) AS numFemales FROM pool;
0
 
SuprSpy79Author Commented:
then i just echo numMales and numFemale?
0
 
snoyes_jwCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now