PDA

View Full Version : php-mysql: query with 2 'Where's on the same column?



eyebum
June 11th, 2009, 05:06 AM
Hey there,
Anyone know if it is possible to make a query function with multiple WHERE clauses that reference the same column?
I have no problem with multiple WHERE clauses referencing different columns, but my query fails when I add in that duplicate reference...Here's some code:


$result = mysql_query("select * from {$table} WHERE Registration.Division = '$Division' && Registration.Division = 'Open' && Event = '$Event' ORDER BY Rorder ASC");

Maybe I am using the wrong syntax. But if I drop that second reference to 'Registration.Division', it works fine.
But of course, I need it!

Thanks,

Chris

icio
June 12th, 2009, 04:11 AM
It's because they contradict each other!

Registration.Division = '$Division' && Registration.Division = 'Open'

If $Division is not equal to 'Open' then Registration.Division cannot be equal to both. Your query requires it is equal to both. Since this equality cannot occur on any row, there are no rows returned.

What are you trying to achieve? Is it that Division equals $Division OR 'Open'?

biznuge
June 12th, 2009, 06:13 AM
Icio's right! Logic FIAL....



WHERE Registration.Division = '$Division' && Registration.Division = 'Open'


the only case for this ever happening would be if $Division='Open' in which case having both of the comparisons would be pointless.

Did you not maybe mean to do an OR rather than an AND for this condition...?

Like...



WHERE (Registration.Division = '$Division' OR Registration.Division = 'Open')


...including the brakcets, so that your OR comparison fires as a comparison before the && comes in for the "Event" comparison.


:2c:

icio
June 12th, 2009, 06:29 AM
You exclaim as if it surprises you. Cheesh... :grin:

biznuge
June 12th, 2009, 07:36 AM
lol

eyebum
June 12th, 2009, 12:17 PM
Icio's right! Logic FIAL....



WHERE Registration.Division = '$Division' && Registration.Division = 'Open'


the only case for this ever happening would be if $Division='Open' in which case having both of the comparisons would be pointless.

Did you not maybe mean to do an OR rather than an AND for this condition...?

Like...



WHERE (Registration.Division = '$Division' OR Registration.Division = 'Open')


...including the brakcets, so that your OR comparison fires as a comparison before the && comes in for the "Event" comparison.

:2c:

Okay, I see the problem, and you are right, it is a logic trainwreck.
But I actually do want the returned results to be from an AND, not OR.

What I have is this: user enters team registration data. This code is used to simply display the registered teams. I have 3 different types of registration possible: OFS, OTC, and Open. I want the display to display OFS AND Open, or OTC AND Open.

This is a simple form submit to select which division. Perhaps I can put the logic in there instead, and direct the form to one of two pages, depending on the choice? Oh, no! Form submits to one php script which determines which division, and then sends it on to the correct php file.

That would work. It is inelegant, as I trade one nice and neat file for at least 3 or 4, but it would work.

Unless, of course, someone can figure out how to do an AND operation on a column!

Thanks,

Chris

eyebum
June 12th, 2009, 07:11 PM
Agh! YES! OK!
I see it. You guys are exactly right. It needs to be an OR!!!

I was telling myself "I need to display all of the 'Open' teams AND all of the 'OFS' teams.

What I really meant was "I need to display all of the teams who are 'Open' OR 'OFS'" !!!!!

Major bigtime 'duh'. I am not a programmer, I am tackling PHP with only a couple of ancient classes in FORTRAN, Pascal and Basic in my background. So the 'programmer's logic' doesn't always come that quickly.

Anyhow, thanks guys, you were exactly right. OR.

Chris

biznuge
June 13th, 2009, 04:55 PM
penny drops! WIN!

good luck man!

icio
June 14th, 2009, 03:37 PM
Glad we could help :thumb:

keceutike
June 17th, 2009, 02:14 PM
Hello.Im a PHP n MySQL n00b learning..so if you dont understand what Im trying to do..Ask me again, so I can try to explain in more clear manner. Thank you for your help, in advance.i would like to have a set of list within a table like this:Code:list a -1 -2 -3 -4list b -1 -2 -3 -4with the numbers on auto_increment, or is this not possible?

biznuge
June 17th, 2009, 03:28 PM
just set up your table in phpmyadmin to be:-

Int - set the "extra" field to "auto_increment" and select the "primary key" radio button to the far right.

then set up any other associated fields and you should be good to go!

hope this helps.