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
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";
  				

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.