Jump to content

Using Count with Where


PhilipK
 Share

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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