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
https://forums.phpfreaks.com/topic/230543-sql-showing-all-listings/
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"

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.