RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1406 | Replies: 2 | Thread Tools  Display Modes
Reply
Join Date: Aug 2005
Posts: 17
Reputation: StrikeFreedom is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
StrikeFreedom StrikeFreedom is offline Offline
Newbie Poster

Need help with search script!

  #1  
Aug 8th, 2005
Hi people,

I have been trying to develop a database searching PHP script and encountered some problem. The problem is that whatever I type in as the keyword the script would return me all the entries from the database regardless of whether the word is actually there or not. Can someone help me please? Below is how I wrote the search script.

[php]<?

//database connection
include 'includes/database.php';

//search query
$sql=mysql_query("SELECT * FROM stories WHERE title LIKE '%".$_POST['keyword']."%'
OR author LIKE '%".$_POST['keyword']."%' OR content LIKE '%".$_POST['keyword']."%'");
$num_results=mysql_num_rows($sql);

//table and loop for displaying results
echo 'You have searched for: '.$keyword.'<p>Number of Results: '.$num_results.'</p>';
echo '<table width="504" border="0" valign="top" cellspacing="0" cellpadding="0">';
echo '<tr class="style1">';
echo '<td width="200"><span class="style4">Title</span></td>';
echo '<td width="100"><strong>Author</strong></td>';
echo '<td width="30"><strong>Vol</strong></td>';
echo '<td width="30"><strong>No</strong></td>';
echo '<td><strong>Intro</strong></td>';
echo '</tr>';


for ($i=0; $i<$num_results; $i++){
$row=mysql_fetch_array($sql);

$id=$row['storynum'];
$t=$row['title'];
$au=$row['author'];
$vol=$row['volume'];
$num=$row['number'];
$fp=$row['first_p'];

echo '<tr>';
echo '<td><a href="article.php?id='.$id.'">'.$t.'</a></td>';
echo '<td>'.$au.'</td>';
echo '<td>'.$vol.'</td>';
echo '<td>'.$num.'</td>';
echo '<td>'.$fp.'</td>';
echo '</tr>';
}
echo '</table>';
?>[/php]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: USA
Posts: 147
Reputation: ashneet is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
ashneet's Avatar
ashneet ashneet is offline Offline
Junior Poster

Solution Re: Need help with search script!

  #2  
Aug 8th, 2005
Hi
I am new to php but i think this will work:
[PHP]<?

//database connection
include 'includes/database.php';


//search query

$query = "SELECT * FROM stories WHERE SOUNDEX(title) = SOUNDEX('$keyword')
OR SOUNDEX(author) = SOUNDEX('$keyword')
OR SOUNDEX(content) = SOUNDEX('$keyword');
$result = mysql_query($query)
or die("Some error");
$nrow = mysql_num_rows($result);


//table and loop for displaying results
echo 'You have searched for: '.$keyword.'<p>Number of Results: '.$nrow.'</p>';
echo '<table width="504" border="0" valign="top" cellspacing="0" cellpadding="0">';
echo '<tr class="style1">';
echo '<td width="200"><span class="style4">Title</span></td>';
echo '<td width="100"><strong>Author</strong></td>';
echo '<td width="30"><strong>Vol</strong></td>';
echo '<td width="30"><strong>No</strong></td>';
echo '<td><strong>Intro</strong></td>';
echo '</tr>';


for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($result);

$id=$row['storynum'];
$t=$row['title'];
$au=$row['author'];
$vol=$row['volume'];
$num=$row['number'];
$fp=$row['first_p'];

echo '<tr>';
echo '<td><a href="article.php?id='.$id.'">'.$t.'</a></td>';
echo '<td>'.$au.'</td>';
echo '<td>'.$vol.'</td>';
echo '<td>'.$num.'</td>';
echo '<td>'.$fp.'</td>';
echo '</tr>';
}
echo '</table>';
?> [/PHP]
also just dont copy paste code exactly u might have to modify it a little
as i tried to fit the code into the code space...
Reply With Quote  
Join Date: Jul 2005
Location: Illinois, USA
Posts: 43
Reputation: Sp!ke is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sp!ke's Avatar
Sp!ke Sp!ke is offline Offline
Light Poster

Re: Need help with search script!

  #3  
Aug 8th, 2005
Umm...Yeah, you kinda forgot a " in there, that's why the code looks all messed up. StrikeFreedom, don't copy and paste that, it's not right, anyway. I haven't done a "search" in a database, per se, but I would definately thing that you need to have the user select either if they're searching Artist, Title, or Content, and another thing, what are the "%"'s for?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the PHP Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:45 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC