Jump to content

Retrieving Count() result


Lassie

Recommended Posts

I want to count the result of a query using count and have an ivalid resource message.

Why is the code in error?

//prepare sql to get total records
  				$getTotal = "SELECT COUNT(*)FROM products WHERE cat_id=$cat_id'";
  				//sunbit query and store results as $totalPix
  				$total = mysql_query($getTotal);
  				$row = mysql_fetch_row($total);
  				$totalPix = $row[0];
  				echo"$totalPix";
  				

I get no result when echo $totalPix;

Link to comment
https://forums.phpfreaks.com/topic/129956-retrieving-count-result/
Share on other sites

Thanks for coming back.

The variable is declared further in the code as shown below.

I am trying to retrieve a set of records based on the catagory id and then display a thumbnail gallery from which the selected image is displayed along with other details.

The gallery could have a lot of images so I want paginate the results.Count is to get the totla no of images/records.

 

//define number of cols in the table
  					define('COLS',3);
  				//set maxium number of records per page
  					define('SHOWMAX',9);
  				
  					
  				//connect to db
  				$connection = db_connect();
  				$cat_id=12;
  				
  				//prepare sql to get total records
  				$getTotal = "SELECT COUNT(*)FROM products WHERE cat_id={'$cat_id'}";
  				//sunbit query and store results as $totalPix
  				$total = mysql_query($getTotal);
  				$row = mysql_fetch_row($total);
  				$totalPix = $row[0];
  				echo"$totalPix";
  				

Change

<?php
  	$getTotal = "SELECT COUNT(*)FROM products WHERE cat_id={'$cat_id'}";
  	//sunbit query and store results as $totalPix
  	$total = mysql_query($getTotal);
  	$row = mysql_fetch_row($total);
  	$totalPix = $row[0];
  	echo"$totalPix";
?>

to

<?php
  	$getTotal = "SELECT COUNT(*) as cnt FROM products WHERE cat_id='$cat_id'";
  	//sunbit query and store results as $totalPix
  	$total = mysql_query($getTotal) or die("Problem with the query: $getTotal<br>" . mysql_error());
  	$row = mysql_fetch_assoc($total);
  	$totalPix = $row['cnt'];
  	echo $totalPix;
?>

 

Ken

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.