WHERE/OR

Post here if you need help with SQL.
Post Reply
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

WHERE/OR

Post by brisk »

Hey guys

In the tutorial there is (using my column/table names):
FROM threads
			WHERE {$fact_where}
			OR  {$quote_where}
			
But I have about 8+ columns, so how do I do the OR bit? I tried WHERE, OR, OR, OR but it doesn't work so what is the correct syntax?

Thanks

Ed
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: WHERE/OR

Post by jacek »

You should be able to OR, OR, OR, OR, OR ye[

try adding
echo mysql_error();
after the failed query and see if it tells you what is wrong.
Image
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Re: WHERE/OR

Post by brisk »

jacek wrote:You should be able to OR, OR, OR, OR, OR ye[

try adding
echo mysql_error();
after the failed query and see if it tells you what is wrong.
I'm getting the "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given" error. I can assure you each column has the word 'green' in it which is the word I'm searching with.
function search_posts($term){
	
	//same as explode instead of character, will take regular expression
	$keywords = preg_split('#\s+#',mysql_real_escape_string($term));
	
	if (empty($keywords)){
		return array();	
	}
	
	$headline_where = "`headline` LIKE '%" . implode("%' OR `headline` LIKE '%", $keywords) . "%'";
	$fact_where = "`fact` LIKE '%" . implode("%' OR `fact` LIKE '%", $keywords) . "%'";
	$idea_where = "`idea` LIKE '%" . implode("%' OR `idea LIKE '%", $keywords) . "%'";	
	$quote_where = "`quote` LIKE '%" . implode("%' OR `quote` LIKE '%", $keywords) . "%'";
		
	$sql = "SELECT 
					
					`headline`
					`fact`
					`idea`
					`quote`			
					
						
			FROM threads
			WHERE {$headline_where}
			OR  {$fact_where}
			OR  {$idea_where}
			OR  {$quote_where}		
			
					
			";
	$result = mysql_query($sql);
	
	$results = array();
	
	while (($row = mysql_fetch_assoc($result)) !== false){
		$results[] = $row;	
	}
					
	return $results;
}
		
	
?>
<?php

	if (empty($_GET['term']) === false){
		print_r(search_posts($_GET['term']));	
		
		echo count($search_results);
		
			if (empty($search_results)){
				echo 'Your search returned no results';				
			}
			
			foreach ($search_results as $results){
				echo "<h3>{results['headline']}</h3>";
				echo "<h3>{results['fact']}</h3>";
				echo "<h3>{results['idea']}</h3>";
				echo "<h3>{results['quote']}</h3>";
			}
	}


?>   
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: WHERE/OR

Post by jacek »

implode("%' OR `idea LIKE '%", $keywords)
You missed a ` after idea.

Hard to spot !
Image
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Re: WHERE/OR

Post by brisk »

jacek wrote:
implode("%' OR `idea LIKE '%", $keywords)
You missed a ` after idea.

Hard to spot !
Wow nice one, sorry for that.

It's still not working :( Getting same error.

Screen shot of my table:

http://img15.imageshack.us/img15/1024/13286720.png

Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: WHERE/OR

Post by Temor »

add
echo mysql_error();
after running your query, but before the return line.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: WHERE/OR

Post by jacek »

In what way is it not working ?
Image
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Re: WHERE/OR

Post by brisk »

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\budgie\Organised2\functions\functions.php on line 486
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 '`idea` `quote` FROM threads WHERE `headline` LIK' at line 5

I missed out the commas for each column after 'SELECT' statement, I put them in but it's still not working proper, it's returning results though. Let me have a go first.
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Re: WHERE/OR

Post by brisk »

brisk wrote:Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\budgie\Organised2\functions\functions.php on line 486
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 '`idea` `quote` FROM threads WHERE `headline` LIK' at line 5

I missed out the commas for each column after 'SELECT' statement, I put them in but it's still not working proper, it's returning results though. Let me have a go first.
All solved :)

Cheers guys.
Post Reply