Jump to content


Photo

COUNT Statement


  • Please log in to reply
3 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 15 July 2006 - 11:23 PM

Hi all

I have a count statement, but it's counting too many rows, can anyone see why?

$results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM images WHERE view = '1' AND type1 = '".$_GET['type1']."' OR type1a = '".$_GET['type1']."' OR type1b = '".$_GET['type1']."' OR type1c = '".$_GET['type1']."' AND type2 = '".$_GET['type2']."' OR type2a = '".$_GET['type2']."' OR type2b = '".$_GET['type2']."' OR type2c = '".$_GET['type2']."'", $connection),0);

Basically there is 2 drop downs in a form, one called 'type1' and another called 'type2', when the form is submitted, the above statement is supposed to check to see if what was selected in 'type1' appears anywhere in the colums

type1 OR
type1a OR
type1b OR
type1c OR

and also check that what was selected in 'type2' appears anywhere in colum

type2 OR
type2a OR
type2b OR
type2c OR

Can anyone help at all?

Thanks in advance

Ed

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 July 2006 - 11:37 PM

you've got to group your conditions based on how you want them processed.  from what i can see, you want the following:

view must = 1
one of the type1s must = $_GET['type1']
one of the type2s must = $_GET['type2']

in this case, you need to group the type1s into parenthesis, and group the type2s into parenthesis.  otherwise you are saying "view must be 1 and type1 must be $_GET['type'] 1, or we can abandon the whole process if type1a is $_GET['type1']."  conditions are parsed left to right (i believe), and will run the statement (in this case, count the row) as soon as the condition is true.

view='1' AND (type1='{$_GET['type1']}' OR ...) AND (type2='{$_GET['type2']}' ...)

give that a shot.

#3 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 16 July 2006 - 11:26 AM

Hi

Thanks for that, I tried adjusting the code to

$results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM mirrorimages WHERE view='1' AND (type1='{$_GET['type1']}' OR type1a='{$_GET['type1']}' OR type1b='{$_GET['type1']}' OR type1c='{$_GET['type1']}') AND (type2='{$_GET['type2']}' OR type2a='{$_GET['type2']}' OR type2b='{$_GET['type2']}' OR type2c='{$_GET['type2']}')", $connection),0);

But that returns 0 no matter how many results should be found.

Can anyone shed anymore light on getting it to count correct?

THanks in advance

Ed

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 17 July 2006 - 03:09 AM

the first question i guess, is are you SURE that there are rows matching all three?  ie. view is 1, one of the type1s is equal to the value from select box 1, and that one of the type2s matches the second select box as well?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users