Page 1 of 1
WHERE/OR
Posted: Mon Feb 13, 2012 12:12 am
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
Re: WHERE/OR
Posted: Mon Feb 13, 2012 12:47 am
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.
Re: WHERE/OR
Posted: Mon Feb 13, 2012 1:10 am
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>";
}
}
?>
Re: WHERE/OR
Posted: Mon Feb 13, 2012 1:12 am
by jacek
implode("%' OR `idea LIKE '%", $keywords)
You missed a ` after idea.
Hard to spot !
Re: WHERE/OR
Posted: Mon Feb 13, 2012 1:17 am
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

Re: WHERE/OR
Posted: Mon Feb 13, 2012 7:14 am
by Temor
add
echo mysql_error();
after running your query, but before the return line.
Re: WHERE/OR
Posted: Mon Feb 13, 2012 2:43 pm
by jacek
In what way is it not working ?
Re: WHERE/OR
Posted: Mon Feb 13, 2012 3:11 pm
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.
Re: WHERE/OR
Posted: Mon Feb 13, 2012 3:32 pm
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.