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.

<?

//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>';
?>

Hi
I am new to php but i think this will work:

<? 

//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>'; 
?>

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...

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.