Forum Moderators: coopster

Message Too Old, No Replies

Problem counting records

         

PupChow

5:10 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



I am trying to count the number of returned records and set it to a variable. But for some reason I have been getting the "Resource id #6" return instead of a number. I am guessing I need to put the returning result into an array... but how do I do that if the returning value is only one number (from the COUNT SQL function)? Many thanks in advance!

[codes]
<?php

require("cooking.php");

$sql = "SELECT * FROM states ORDER BY name";
$resultID = mysql_query($sql, $linkID);

// Looping through the States
while ($row = mysql_fetch_assoc($resultID))
{
$ThisState = $row[abbrev];
$ThisStateName = $row[name];

// Getting record of current State
$sqlcount = "SELECT COUNT(*) FROM troupes WHERE State = '".$ThisState."'";
$CountTroupes = mysql_query($sqlcount, $linkID);

echo $ThisStateName . " (".$CountTroupes.")<br />";
}

mysql_close($linkID);
?>
[/codes]

jatar_k

5:27 pm on Apr 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the same as yoou would any other result I would pull the count to a named var though

// Getting record of current State
$sqlcount = "SELECT COUNT(colname) as totalrows FROM troupes WHERE State = '".$ThisState."'";
$ct = mysql_query($sqlcount, $linkID);
if ($row = mysql_fetch_array($ct)) $CountTroupes = $row['totalrows'];

PupChow

6:19 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



Perfect! Thank you so much! Is it normal practice to create a new colum (var?) for the count? I was under the impression that simply by running the SQL statement I will get the return number... sorry, trying to understand this. =)

jatar_k

6:25 pm on Apr 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I find it easier to grab the value that way