Jacobb123
01-31-2008, 07:00 AM
I have the following code for a simple search but it is not working and have a couple questions because I think the way I have it setup now is not what I am needing. I am trying to do a simple search that will pull keywords from a table and return the results. The problem is that it works only if the search term is at the beginning of the string. I am not sure if I can use LIKE in this manner. Should I use REGEX to match the string?
here is the code echo '<form action="blog_search.php" method="get" name="search"><input name="search_term" type="text"><input name="submit" type="submit" value="Submit"></form>';
if(isset($_GET['search_term'])){
$sql =mysql_query("SELECT * FROM blogs WHERE body LIKE '{$_GET['search_term']}%'") or die(mysql_error());
}
else{
$sql =mysql_query("SELECT * FROM blogs") or die(mysql_error());
}
$i=0;
while($result=mysql_fetch_array($sql)){
$text=substr($result['subject'],0,100);
$length=strlen($result['body']);
$limit="250";
if($length > $limit){
$body=substr($result['body'],0,250).'...';
}
else{
$body=substr($result['body'],0,250);
}
echo '<table width="75%" border="0" CELLSPACING="0">
<tr>
<td><a href="view_blog.php?id='.$result['id'].'">'.$text.'</a><br></td>
</tr>
<tr>
<td><br></td>
</tr>
<tr>
<td >'.$body.'</td>
</tr>
</table><br>';
$i++;
}
here is the code echo '<form action="blog_search.php" method="get" name="search"><input name="search_term" type="text"><input name="submit" type="submit" value="Submit"></form>';
if(isset($_GET['search_term'])){
$sql =mysql_query("SELECT * FROM blogs WHERE body LIKE '{$_GET['search_term']}%'") or die(mysql_error());
}
else{
$sql =mysql_query("SELECT * FROM blogs") or die(mysql_error());
}
$i=0;
while($result=mysql_fetch_array($sql)){
$text=substr($result['subject'],0,100);
$length=strlen($result['body']);
$limit="250";
if($length > $limit){
$body=substr($result['body'],0,250).'...';
}
else{
$body=substr($result['body'],0,250);
}
echo '<table width="75%" border="0" CELLSPACING="0">
<tr>
<td><a href="view_blog.php?id='.$result['id'].'">'.$text.'</a><br></td>
</tr>
<tr>
<td><br></td>
</tr>
<tr>
<td >'.$body.'</td>
</tr>
</table><br>';
$i++;
}