Is there a problem with the or operator in mysql?

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
Sheridan
Forum Newbie
Posts: 18
Joined: Sat May 31, 2008 1:50 pm

Is there a problem with the or operator in mysql?

Post by Sheridan »

I am trying to execute an mysql query of the form:

Code: Select all

 
 $query = "Select * from individuals where MotherId like '%".$iName."%' or  FatherID like '%".$iName."%' "; 
 
I am getting an "unexpected FatherID" error message. FatherID is of course an attribue of the queried table. The query works as long as I do not have the OR condidtion. That is, the following query works.

Code: Select all

 
 $query = "Select * from individuals where MotherId like '%".$iName."%' ";  
 


What am I doing wrong? :(
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Is there a problem with the or operator in mysql?

Post by Jade »

Sheridan wrote:

Code: Select all

 
$query = "Select * from individuals where MotherId like '%".$iName."%' or FatherID like '%".$iName."%' "; 
 
Try:

Code: Select all

 
$query = "Select * from individuals where MotherId like '%$iName%' or FatherID like '%$iName%'";
 
Only other thing I can think either $iName has weird characters in it (if so escape them first) or fatherID is for some reason a reserved keyword in mysql.
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Is there a problem with the or operator in mysql?

Post by dbemowsk »

What is the exact error you are getting?

I should be able to help you further once I see the exact error.

I do see though that you have "Motherid" and "FatherID". Should the "FatherID" be "Fatherid"? That can make a difference as field names are case sensitive.
____________________________________________________

Jade, he is not incorrect in the way the query is written. He is using string concatenation which is perfectly fine. I actually prefer to do it that way versus using the variables inside the quotes because I use a syntax highlighting editor which allows me to better see where I have variables being inserted into strings. I see it like this:

$query = "Select * from individuals where MotherId like '%".$iName."%' or FatherID like '%".$iName."%' ";

versus like this

$query = "Select * from individuals where MotherId like '%$iName%' or FatherID like '%$iName%' ";
Post Reply