Page 1 of 1

search

Posted: Sat Feb 12, 2005 6:29 pm
by shiznatix
ok yes i have searched the forums for this but couldnt find anything usefull.

i have a table with a field called title. i want to have a search function to find titles that are similar to what was entered into the search or titles that contain the keyword. i have tried

Code: Select all

mysql_query("SELECT * FROM threads WHERE title LIKE '$keyword'");
but that only gives results to exact matches it seams. im not sure how to go about doing this.

Posted: Sat Feb 12, 2005 6:31 pm
by s.dot
make it like this

Code: Select all

mysql_query("SELECT * FROM threads WHERE title LIKE '%$keyword%'");

Posted: Sat Feb 12, 2005 7:06 pm
by shiznatix
ok. spot the error?

Code: Select all

echo 'Threads Matching This Search:<br>';
	$search_thread = mysql_query("SELECT * FROM threads WHERE title LIKE '%$keyword%'");
	while ($result_thread = mysql_fetch_assoc($search_thread))&#123;
	echo $result_thread&#1111;'title'].'<br>';
	&#125;
	
	echo '<br>Posts Matching This Search:<br>';
	$search_post = mysql_query("SELECT * FROM posts WHERE post LIKE '%keyword%'");
	while ($result_post = mysql_fetch_assoc($search_post))&#123;
	echo $result_post&#1111;'post'].'<br>';
	&#125;
the threads search works perfectly, the title is set as a varchar() but the posts search dosnt work at all. its set as text. why

...

Posted: Sat Feb 12, 2005 7:15 pm
by s.dot
The field type shouldn't matter.

I'd make sure that your table is called "posts" and your table column is called "post".

I don't see any errors. ;/

Posted: Sat Feb 12, 2005 7:20 pm
by shiznatix
im sure i have spelled everything correctly, 100% sure. anyone have any idea why this isnt working?
the full code is this

Code: Select all

<? ob_start(); include('config.php'); ?>
<form action='search.php' method='get'>
<input type='text' name='keyword' size='15'>
<input type='submit' name='submit' value='Search'>
</form>
<?
if (isset($_GET&#1111;'keyword']))&#123;
$keyword = $_GET&#1111;'keyword'];

	echo 'Threads Matching This Search:<br>';
	$search_thread = mysql_query("SELECT * FROM threads WHERE title LIKE '%$keyword%'");
	while ($result_thread = mysql_fetch_assoc($search_thread))&#123;
	echo $result_thread&#1111;'title'].'<br>';
	&#125;
	
	echo '<br>Posts Matching This Search:<br>';
	$search_post = mysql_query("SELECT * FROM posts WHERE post LIKE '%keyword%'");
	while ($result_post = mysql_fetch_assoc($search_post))&#123;
	echo $result_post&#1111;'post'].'<br>';
	&#125;

&#125;
?>

..

Posted: Sat Feb 12, 2005 7:32 pm
by s.dot
ahhh I spot the error

in your post results you have LIKE '%keyword%'
it should be LIKE '%$keyword%'

Posted: Sat Feb 12, 2005 7:34 pm
by shiznatix
:oops: haha my bad. thanks buddy