Results 1 to 2 of 2

Thread: MySQL query WHERE AND OR

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    MySQL query WHERE AND OR

    Hello, here is a query that is executed when the search button is clicked.

    Code:
    $query = "SELECT * FROM sites WHERE region LIKE '%$searchContent%' OR banner LIKE '%$searchContent%' OR site LIKE '%$searchContent%' OR comment LIKE '%$searchContent%' OR lastupdated LIKE '%$searchContent%' AND company='$company'";
    As you see it searches for $searchContent using WHERE...LIKE...OR in all the columns listed. The last part of the query is the final condition, AND company='$company'.

    Basicly the user can search all the columns as long as the column company is eqaul to a variable called $company. This is not working, instead AND company='$company' is being ignored. The user has access to all the columns.

    Is it that fact that AND company='$company' is at the end of the query after all the OR's? How would I fix this up?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: MySQL query WHERE AND OR

    by using parenthesis to force the order of operations to your will....


    Code:
    $query = "SELECT * FROM sites WHERE ((region LIKE '%$searchContent%') OR (banner LIKE '%$searchContent%') OR (site LIKE '%$searchContent%') OR (comment LIKE '%$searchContent%') OR (lastupdated LIKE '%$searchContent%')) AND (company='$company')";
    With the parenthesis I added, it will return where any ONE of the or clauses is true, AND the company matches.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width