search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

search

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

make it like this

Code: Select all

mysql_query("SELECT * FROM threads WHERE title LIKE '%$keyword%'");
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

...

Post 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. ;/
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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;
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

..

Post by s.dot »

ahhh I spot the error

in your post results you have LIKE '%keyword%'
it should be LIKE '%$keyword%'
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

:oops: haha my bad. thanks buddy
Post Reply