Query not working as expected

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
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Query not working as expected

Post by diseman »

Code: Select all

$name=$_POST['affiliate_search_for_users'];
	
	$query = "SELECT b_firstname, b_lastname, b_state, username, b_phone_m FROM contact_info WHERE affiliate = ' ".$affiliate." ' &&  
									
        b_firstname 	LIKE '%" . $name ."%' OR
	b_lastname 	LIKE '%" . $name ."%' OR
	b_phone_h 	LIKE '%" . $name ."%' OR
	b_phone_m 	LIKE '%" . $name ."%' OR
	b_phone_w 	LIKE '%" . $name ."%' ";

$result = mysql_query($query) or die(mysql_error());
This is a search box search. When I put a name/user in the box to search it's good, but when I put a name of a user that belongs to a different $affiliate, it pulls the record and displays it! I expressly put in there " WHERE affiliate = ' " .$affiliate. " ' , so I shouldn't be getting any records that belong to a different $affiliate.

I've even test " print $affiliate ; " and it shows me the proper result, so the correct variable is being used.

This thing has been handing me my ass for hours. Can anyone out there help a beginner?

Thanks...

Basically, I want to search an affiliate's records based on firstname, lastname, etc..., but the record MUST belong to that affiliate.
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: Query not working as expected

Post by internet-solution »

You need brackets.

Code: Select all

$query = "SELECT b_firstname, b_lastname, b_state, username, b_phone_m FROM contact_info WHERE affiliate = ' ".$affiliate." ' &&  
                                                                       
        (b_firstname     LIKE '%" . $name ."%' OR
        b_lastname      LIKE '%" . $name ."%' OR
        b_phone_h       LIKE '%" . $name ."%' OR
        b_phone_m       LIKE '%" . $name ."%' OR
        b_phone_w       LIKE '%" . $name ."%' )";
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Query not working as expected

Post by diseman »

Huh. So simple. Wish I had asked the question 3 fricken' hours ago! : )

Thanks AGAIN Internet-Solution!

Hey, if you have the time and inclination... take a look at:

viewtopic.php?f=1&t=121306

That one is stumping me too
Post Reply