PHP Web Host - Quality Web Hosting For All PHP Applications Clan Themes! We make clans look good!!
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Wed Dec 06, 2006 9:06 pm Reply with quote Back to top

Having a problem with OR sticking it's or in

I'm trying to get this query to search two fields, it seems to work but I think It's changing the AND to an OR.

Code:

$query = "SELECT date,title,description FROM " . $prefix . "_news  " .
"WHERE description LIKE '%".$keywords['0']."%' OR title LIKE '%".$keywords['0']."%'";
for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND description LIKE '%".$keywords[$i]."%'";
$query = $query." AND title LIKE '%".$keywords[$i]."%' ";
}
$query = $query." ORDER BY date DESC "." LIMIT $offset, $rowsPerPage";
$result2 = mysql_query($query) or die(mysql_error());
$keywords = explode(" ", $search);
$query = "SELECT COUNT(date) AS numrows FROM " . $prefix . "_news  " .
"WHERE description LIKE '%".$keywords['0']."%' OR title LIKE '%".$keywords['0']."%' ";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND description LIKE '%".$keywords[$i]."%'";
$query = $query." AND title LIKE '%".$keywords[$i]."%' ";
}


hope you can help
View user's profile Send private message Visit poster's website
Raven
Site Admin/Owner


Joined: Aug 27, 2002
Posts: 16987
Location: Kansas

PostPosted: Wed Dec 06, 2006 9:28 pm Reply with quote Back to top

That cannot and does not happen Wink
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Thu Dec 07, 2006 5:09 am Reply with quote Back to top

Do you mean the code or that AND cannot become OR?

If I do a search for a known title, somehow I'm getting the title plus other OR results.
View user's profile Send private message Visit poster's website
montego
Site Admin


Joined: Aug 29, 2004
Posts: 9136
Location: Arizona

PostPosted: Thu Dec 07, 2006 6:16 am Reply with quote Back to top

How come your for loop is adding other keywords using "AND" if you want these "OR"d?
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Thu Dec 07, 2006 6:21 am Reply with quote Back to top

I want them to be AND but I'm getting OR results
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Thu Dec 07, 2006 6:25 am Reply with quote Back to top

This only happens when I add a second field to search so i'm thinking my SELECT is wrong
View user's profile Send private message Visit poster's website
montego
Site Admin


Joined: Aug 29, 2004
Posts: 9136
Location: Arizona

PostPosted: Thu Dec 07, 2006 6:33 am Reply with quote Back to top

Try something like this instead:

WHERE (description LIKE '%keyword0%' OR title LIKE '%keyword0%) AND (description LIKE '%keyword1%' OR title LIKE '%keyword1%) AND ...
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Thu Dec 07, 2006 8:57 am Reply with quote Back to top

I'm prety sure that stoped the problem with OR but I didn't get any results from title
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Thu Dec 07, 2006 9:10 am Reply with quote Back to top

I see, that's made everything into AND.
View user's profile Send private message Visit poster's website
evaders99
Former Moderator in Good Standing


Joined: Apr 30, 2004
Posts: 3221

PostPosted: Thu Dec 07, 2006 11:18 am Reply with quote Back to top

Yep the order of operations is changed with the parenthesis. You should be careful when mixing AND and OR - parenthesis makes it easier to figure out
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Thu Dec 07, 2006 4:20 pm Reply with quote Back to top

I think I have it, still testing
Code:
"WHERE (description LIKE '%$keywords[0]%' || title LIKE '%$keywords[0]%')";
View user's profile Send private message Visit poster's website
montego
Site Admin


Joined: Aug 29, 2004
Posts: 9136
Location: Arizona

PostPosted: Fri Dec 08, 2006 6:53 am Reply with quote Back to top

Well, || is the same as OR, so not sure that is going to help you. You really have to think out what it is you want this to do. Use the parenthesis to make it easier to see the logic.

Post here in words what a typically query should look like, results-wise. Good logic always starts with a good statement of what I call the "use model".
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Fri Dec 08, 2006 7:43 am Reply with quote Back to top

It looks to be working ok, I guess replacing OR with || eradicates the confusion between AND and OR

The idea is to find any number of keywords (using AND) in the table fields 'title' and/or 'description' (not AND/OR)

Example

(Search 'title' AND 'word1') also search ('description' AND 'word1')
then
(Search 'title' AND 'word2') also search ('description' AND 'word2')
View user's profile Send private message Visit poster's website
djmaze
Subject Matter Expert


Joined: May 15, 2004
Posts: 689
Location: http://tinyurl.com/5z8dmv

PostPosted: Fri Dec 08, 2006 8:34 am Reply with quote Back to top

Code:

SELECT * FROM table
WHERE title LIKE IN ('word1', 'word2')
OR description LIKE IN ('word1', 'word2')


Code:

SELECT * FROM table
WHERE (title LIKE '%word1%' OR title LIKE '%word2%')
OR (description LIKE '%word1%' OR description LIKE '%word2%')

Only registered users can see links on this board!
Get registered or login to the forums!
Only registered users can see links on this board!
Get registered or login to the forums!
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Fri Dec 08, 2006 2:20 pm Reply with quote Back to top

ok I've gone for boolean but still can't get OR and AND to work together without getting OR results.

It may be better to use OR with BOORLEAN sorting the results, that means more reading.

This works ok, is using || a problem or wrong ?

Code:
$query = "SELECT date,title,description FROM " . $prefix . "news  " .
"WHERE MATCH (description,title) AGAINST('%$keywords[0]%' IN BOOLEAN MODE)";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND (description LIKE '%$keywords[$i]%' OR title LIKE '%$keywords[$i]%')";
}
$query = $query." ORDER BY date DESC LIMIT $offset, $rowsPerPage";
View user's profile Send private message Visit poster's website
hinksta
Worker
Worker


Joined: Dec 23, 2005
Posts: 226
Location: UK

PostPosted: Fri Dec 08, 2006 2:37 pm Reply with quote Back to top

and then as I read the post back I looked up and saw djmaze's OR

That must have been the only way I didn't try
View user's profile Send private message Visit poster's website
montego
Site Admin


Joined: Aug 29, 2004
Posts: 9136
Location: Arizona

PostPosted: Sun Dec 10, 2006 9:46 am Reply with quote Back to top

Yes, dj's second example could work as well. But, really, is the bottom line here that you really want to find the keywords in title or in description? If so, there is no reason why you cannot just string them altogether to straight ORs like this:

WHERE title LIKE '%keyword[0]%'
OR title LIKE '%keyword[1]%'
OR title LIKE '%keyword[n]%'
OR description LIKE '%keyword[0]%'
OR description LIKE '%keyword[1]%'
OR description LIKE '%keyword[n]%'
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2011 by Raven

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::
:: fisubice Theme Modified by the RavenNuke™ Team ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum