Jump to content

WHERE LIKE Issues


shanejeffery86

Recommended Posts

Hey all.

 

Let me give you the code first before I start explaining:

 

$query = "SELECT phpfox_user.id, phpfox_user.type, phpfox_user.user, phpfox_user.email, phpfox_user.reg_id, 
phpfox_user.fullname, phpfox_conference_passes.pass_name FROM phpfox_user 
LEFT JOIN phpfox_user_passes ON (phpfox_user.user = phpfox_user_passes.user)
LEFT JOIN phpfox_conference_passes ON (phpfox_user_passes.conference_id = phpfox_conference_passes.conference_id) 
AND (phpfox_user_passes.pass_id = phpfox_conference_passes.pass_id) WHERE phpfox_user.email LIKE '%$search_email%' 
AND phpfox_user.fullname LIKE '%$search_fullname%' AND phpfox_user.reg_id=$search_regid 
AND phpfox_conference_passes.pass_name LIKE '%$search_passtype%' 
ORDER BY phpfox_user.email";

$user_count = mysql_query($query);

 

Here is what is happening --

 

Whenever the $search_regid has a value, the query executes fine.  Also, the $search_regid cannot have a LIKE in the query because it pulls back the wrong results. 

 

Now, whenever $search_fullname or $search_email or $search_passtype have values (the proper values to return query results mind you), the query does not return anything and instead errors out.

 

Error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND phpfox_conference_passes.pass_name LIKE '%One Reg%' ORDER BY phpfox_use' at line 7

 

Any ideas what is wrong with my query?

 

Thanks!

 

Link to comment
Share on other sites

WHERE conditions don't work too well when they apply to the right table of a LEFT JOIN. They need to be part of the join condition.

 

eg

SELECT A.blah, B.foo, B.bar
FROM A
LEFT JOIN B ON A.id = B.aID
WHERE A.blah = 'avalue'  AND B.foo = 'something'

 

should be

SELECT A.blah, B.foo, B.bar
FROM A
LEFT JOIN B ON A.id = B.aID AND B.foo = 'something'
WHERE  A.blah = 'avalue'  

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.