100% possitive this should work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

100% possitive this should work

Post by orchid1 »

this is killing me because it should definitly work but it does not
Im simply trying to add a "AND" conditional to a sql query before a "LIKE" conditional

Code: Select all

 
//this is the basic set up in generic form so you can see it with a little less code and grasp the concept 
 
        $sWhere = "WHERE status = 'Active' AND label LIKE  '%".mysqli_real_escape_string($connect, $xVariable)."%' OR ".
                        "status = 'Active' AND point LIKE '%".mysqli_real_escape_string($connect, $x2Variable )."%' OR ".
                        "status = 'Active' AND city LIKE '%".mysqli_real_escape_string($connect, $y2variable)."%'; ";
    }
    
    $sQuery = "
        SELECT * 
        FROM   camp 
        $sWhere 
    ";
 
//this is the real form
 
 
        $sWhere = "WHERE status = 'Active' AND label LIKE '%".mysqli_real_escape_string($gaSql['link'], $_GET['sSearch'] )."%' OR ".
                        "status = 'Active' AND point LIKE '%".mysqli_real_escape_string($gaSql['link'], $_GET['sSearch'] )."%' OR ".
                        "status = 'Active' AND city LIKE '%".mysqli_real_escape_string($gaSql['link'], $_GET['sSearch'] )."%'; ";
    }
    
    $sQuery = "
        SELECT *
        FROM   camp 
        $sWhere 
    ";
 
 
query works great when I don't incorporate the "AND" conditional statement
basicly the AND statement isn't even considered I don't get an error or anything
it basicly ignores it so it could have it or not have and the statement is overlooked entirely

any ideas???
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

Re: 100% possitive this should work

Post by orchid1 »

to be clearer
the AND statement has no effect on the query result

if I add the AND statement I get the exact same result as the statement when I remove the AND conditional


very frusterated

please help
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: 100% possitive this should work

Post by cpetercarter »

You need some brackets, I think!

Code: Select all

WHERE
(status = 'a' AND label LIKE 'x') OR
(status = 'b' AND point LIKE 'y') OR
(status = 'c' AND city LIKE 'z')
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

Re: 100% possitive this should work

Post by orchid1 »

I think i tried that "Brackets" already I might try it in the morning again when I have a fresh head on my shoulders will get back to this by tomorrow evening
thank you for the idea i think I have a new approach I might just cut it up the code some more almost like exploding it and combining the little parts again
I find that this helps more often then it should :dubious:

I'll post again tomorrow evening PST
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: 100% possitive this should work

Post by VladSun »

cpetercarter wrote:You need some brackets, I think!

Code: Select all

WHERE
(status = 'a' AND label LIKE 'x') OR
(status = 'b' AND point LIKE 'y') OR
(status = 'c' AND city LIKE 'z')
Nope. It's the same as
[sql]WHERESTATUS = 'a' AND label LIKE 'x' ORSTATUS = 'b' AND point LIKE 'y' ORSTATUS = 'c' AND city LIKE 'z'[/sql]
because the AND operator has higher precedence than the OR operator.

@orchid1
if I add the AND statement I get the exact same result as the statement when I remove the AND conditional
Maybe because the status of all records is set to 'Active'?

Your query should work, though it could be rewritten in a shorter form:

Code: Select all

"WHERE 
   status = 'Active' 
   AND 
   (
      label LIKE  '%".mysqli_real_escape_string($connect, $xVariable)."%' 
      OR 
      point LIKE '%".mysqli_real_escape_string($connect, $x2Variable )."%' 
      OR 
      city LIKE '%".mysqli_real_escape_string($connect, $y2variable)."%'; 
   )
";
PS: Please, use [ sql ], [/ sql] or [ php ] [/ php ] BB code tags instead of [ code ] BB code tags.
There are 10 types of people in this world, those who understand binary and those who don't
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

Re: 100% possitive this should work

Post by orchid1 »

the last comment worked

thought I had tried this already

maybe I'm just too tired or i left a dot out or something maybe a ' or " :?:
who know thanks for the help
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: 100% possitive this should work

Post by prometheuzz »

orchid1 wrote:the last comment worked

thought I had tried this already
...
Obviously not!
Post Reply