Jump to content

SQL showing all listings


petenaylor

Recommended Posts

Hi all

 

I have a database where business listing has a table value called live and a value of 1 if it is to be shown on the website. This SQL query works great:

 

$getfeaturedlistings = mysql_query("SELECT * FROM `listings` WHERE live = 1 AND featured = 1 AND $trimmedsqlarea = 1 AND $trimmedcategory = 1 ORDER BY ID DESC");

 

However, in the search script I am using this query:

 

$getfeaturedlistings = mysql_query("SELECT * FROM `listings` WHERE live = 1 AND featured = 1 AND description LIKE \"%$trimmed%\" OR title LIKE \"%$trimmed%\" OR address LIKE \"%$trimmed%\" OR website LIKE \"%$trimmed%\" OR email LIKE \"%$trimmed%\" OR phone LIKE \"%$trimmed%\" ORDER BY id ASC");

 

But this brings back listings that are not live? (Where live is "0" in the SQL)

 

Please help!

 

Many thanks

 

Pete

Link to comment
Share on other sites

You're having precedence issues. I suspect you want to retrieve records that meet both of the first 2  `=` conditions, AND any one or more of the LIKE conditions, correct?

 

If so, you need to add some parentheses to force precedence of the operators. They also make it easier to read . . . :)

 

"SELECT * FROM `listings` 
WHERE 
( live = 1 AND featured = 1 ) 
AND 
(description LIKE \"%$trimmed%\" OR title LIKE \"%$trimmed%\" OR address LIKE \"%$trimmed%\" OR website LIKE \"%$trimmed%\" OR email LIKE \"%$trimmed%\" OR phone LIKE \"%$trimmed%\") 
ORDER BY id ASC"

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.