Lassie Posted October 24, 2008 Share Posted October 24, 2008 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 More sharing options...
wrathican Posted October 24, 2008 Share Posted October 24, 2008 your missing an apostrophe on the where clause also try putting curly braces '{' & '}' before and after your variable like this: cat_id='{$cat_id}' also where are you declaring the variable? Link to comment https://forums.phpfreaks.com/topic/129956-retrieving-count-result/#findComment-673706 Share on other sites More sharing options...
Lassie Posted October 24, 2008 Author Share Posted October 24, 2008 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 https://forums.phpfreaks.com/topic/129956-retrieving-count-result/#findComment-673758 Share on other sites More sharing options...
Lassie Posted October 24, 2008 Author Share Posted October 24, 2008 Still no result though Link to comment https://forums.phpfreaks.com/topic/129956-retrieving-count-result/#findComment-673763 Share on other sites More sharing options...
kenrbnsn Posted October 24, 2008 Share Posted October 24, 2008 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 https://forums.phpfreaks.com/topic/129956-retrieving-count-result/#findComment-673768 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.