Problem Counting Rows

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Bert
Forum Newbie
Posts: 6
Joined: Sat Dec 07, 2002 12:26 pm
Location: California, USA

Problem Counting Rows

Post by Bert »

I'm trying to count how many rows I have in a table but I always get this error.
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/iwinall/public_html/clan/members/memberslist.php on line 14
Here's my code

Code: Select all

<?php
$pagename = "Members List";
include("includes/header.php");

//Connect
$db = mysql_connect("localhost","bert","**********");
mysql_select_db("bert",$db);

//Querying
$requete = "SELECT * FROM members";
 $result = mysql_query ($requete,$db);
 $article = mysql_fetch_object($result);
 mysql_free_result($result);
 $row = mysql_num_rows($result);

//Display
print("<table border=1>
<tr>
 <td><b>Name:</b></td>
 <td><b>Rank:</b></td>
 <td><b>Points:</b></td>
 <td><b>Positions:</b></td>
 <td><b>Certificates:</b></td>
 <td><b>Awards:</b></td>
 <td><b>Join Date:</b></td>
</tr>
");

$count=0;
while($count!=$row){
print("
<tr>
 <td>$article->name</td>
 <td>$article->rank</td>
 <td>$article->points</td>
 <td>$article->positions</td>
 <td>$article->certificates</td>
 <td>$article->awards</td>
 <td>$article->jdate</td>
</tr>
");
$count++;};

print("</table>");

include("includes/footer.php");
?>
?>
Line 14 would be

Code: Select all

$row = mysql_num_rows($result);
?>
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

the row doesn't exist anymore by the time you call mysql_num_rows() since you called mysql_free_result($result); get rid of that and you should be ok. :)
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

You could also do:

$sql = "SELECT COUNT(*) INTO MyVar";


Phil J.
Post Reply