Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

mysql_query

Posted on 2006-05-24
6
Medium Priority
?
254 Views
Last Modified: 2006-11-18
Hi, My problem is that in following query I get results from "beloning" that is not  beloning='$typBeloning'. Why? And how do I write the query right?

$data = mysql_query("SELECT * FROM gastro_beloning WHERE beloning='$typBeloning' AND namn1 LIKE '$strangBeloning%' OR namn2 LIKE '$strangBeloning%' OR namn3 LIKE '$strangBeloning%' OR kallad LIKE '$strangBeloning%' OR namn1 LIKE '%-$strangBeloning%' OR namn1 LIKE '% $strangBeloning%' OR namn2 LIKE '% $strangBeloning%' OR namn3 LIKE '% $strangBeloning%' OR kallad LIKE '% $strangBeloning%' ORDER BY beloning, ar, namn1, foretag");
0
Comment
Question by:kalleanka
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
crnz earned 750 total points
ID: 16757724
You need to put brackets around your ANDs and ORs to ensure you get the exact behavoir you want.  Can you express in English what result you are after?  I can then possibly detect where the brackets need to go.  Not having brackets then counts on the particular internal prescedence of ANDs and ORs, and when you mix them up like that, even adding brackets for shear readability is a good idea, not counting the fact that you then do not count on the internal prescendence of AND and OR operators.
0
 
LVL 1

Expert Comment

by:dktu
ID: 16757768
solution here :

$data = mysql_query("SELECT * FROM gastro_beloning WHERE beloning='{$typBeloning}' AND namn1 LIKE '{$strangBeloning}%' OR namn2 LIKE '{$strangBeloning}%' OR namn3 LIKE '{$strangBeloning}%' OR kallad LIKE '{$strangBeloning}%' OR namn1 LIKE '%-{$strangBeloning}%' OR namn1 LIKE '% {$strangBeloning}%' OR namn2 LIKE '% {$strangBeloning}%' OR namn3 LIKE '% {$strangBeloning}%' OR kallad LIKE '% {$strangBeloning}%' ORDER BY beloning, ar, namn1, foretag");
0
 
LVL 1

Author Comment

by:kalleanka
ID: 16757782
oh???

I got it to work as I wanted with brackets like this:

$data = mysql_query("SELECT * FROM gastro_beloning WHERE beloning='$typBeloning' AND (namn1 LIKE '$strangBeloning%' OR namn2 LIKE '$strangBeloning%' OR namn3 LIKE '$strangBeloning%' OR kallad LIKE '$strangBeloning%' OR namn1 LIKE '%-$strangBeloning%' OR namn1 LIKE '% $strangBeloning%' OR namn2 LIKE '% $strangBeloning%' OR namn3 LIKE '% $strangBeloning%' OR kallad LIKE '% $strangBeloning%') ORDER BY beloning, ar, namn1, foretag");
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:crnz
ID: 16757802
That looks good to me...

The { } brackets are to make the query more robust, but aren't always needed.

0
 
LVL 1

Author Comment

by:kalleanka
ID: 16757807
just adding, dktu's solution didn't work, so the points go to crnz
0
 
LVL 2

Expert Comment

by:crnz
ID: 16757905
Thank you.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question