|
Posted by CJ Llewellyn on 06/20/05 23:17
On Mon, 20 Jun 2005 11:39:15 -0700, Bryan wrote:
> I've been trying to work out a problem but I'm having no luck. I have
> field in my table for area and I need to count the number of matches
> and assign a variable for each one.. this is what I have but it doesn't
> work.. any ideas?
>
> $testcount = mysql_query("select area, count(area) as newcount from rea
> group by area order by area");
> while ($returncount = mysql_fetch_array($testcount)) {
> if ($returncount[area] == 'RCC') {
> $rccn = $returncount[newcount];
> }
> if ($returncount[area] == 'RCL') {
> $rccl = $returncount[newcount];
> }
> if ($returncount[area] == 'RCH') {
> $rcch = $returncount[newcount];
> }
> if ($returncount[area] == 'RCW') {
> $rccw = $returncount[newcount];
> }
> if ($returncount[area] == 'RNK') {
> $rnkn = $returncount[newcount];
> }
> if ($returncount[area] == 'RNK') {
> $rnen = $returncount[newcount];
> }
> }
Like Andy said, try some basic error checking first
// always initialise variables before you use them
$aAreas = array();
$sql = "select area, count(area) as newcount from rea group by area order
by area";
$results = mysql_query($sql , $conn); if(! $results || mysql_error($conn)
|| mysql_num_rows($results)<1) {
echo "Unable to get results [$sql] : " . mysql_error($conn);
}
else
{
// why use 10 variables when 1 will do?
while($row = mysql_fetch_array($results)) {
$aAreas[$row[area]] = $row[newcount];
}
// $aAreas will now hold your results with the extra
print_r($aAreas) . "<br>\n";
}
Navigation:
[Reply to this message]
|