Jump to content

Using Count with Where


PhilipK

Recommended Posts

Hello, I'm using the following code to count the amount of SQL results and divide it. Works great as is but I want to alter it so it works with categories.

 

$amount = mysql_query('SELECT COUNT(`car_id`) AS num FROM `tbl_cars`') or die(mysql_error());

$amount_row = mysql_fetch_assoc($amount);

 

I tried adding...

 

WHERE car_cat = '".$cat."'

 

which causes an error.

 

Why doesn't this work, and how can I get the amount of results as a variable?

Link to comment
https://forums.phpfreaks.com/topic/230513-using-count-with-where/
Share on other sites

Ok here is the code after my attempt.

 

$amount = mysql_query('SELECT COUNT(`car_id`) AS num FROM `tbl_cars` WHERE car_cat = '".$cat."'') or die(mysql_error());

$amount_row = mysql_fetch_assoc($amount);

 

It causes a syntax error in Dreamweaver and server error.

If this is what is really in your code:

<?php
$amount = mysql_query('SELECT COUNT(`car_id`) AS num FROM `tbl_cars` WHERE car_cat = '".$cat."'') or die(mysql_error());
?>

you have a quote problem. It should be

<?php
$amount = mysql_query("SELECT COUNT(`car_id`) AS num FROM `tbl_cars` WHERE car_cat = '$cat'") or die(mysql_error());
?>

 

Ken

Thanks Ken that fixed the problem.

 

I now tried using your structure for an alteration for a search string.

 

<?php
$amount = mysql_query("SELECT COUNT(`car_id`) AS num FROM `tbl_cars` WHERE car_name LIKE '%$srch%' OR car_year LIKE '%$srch%' OR car_cat LIKE '%$srch%'") or die(mysql_error());
?>

 

Is it a similar problem?

 

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.