Page 1 of 1

Problem Counting Rows

Posted: Fri Jan 10, 2003 10:00 pm
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);
?>

Posted: Fri Jan 10, 2003 10:59 pm
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. :)

Posted: Sat Jan 11, 2003 12:10 pm
by fractalvibes
You could also do:

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


Phil J.