cs1h Posted September 21, 2007 Share Posted September 21, 2007 Hi, I have a search script for my site which at the moment selects results on the outcome of three criteria. First it gets the right country, then the right article type and then it matches a keyword to the results from the first two criteria to find the results. But what I would like it to do now is search for the keyword in two fields in the mysql table, if it finds the keyword in ether field or both I would like it to show it in the results. But I am not sure how to do this, My script so far is, <? $targetb = $_POST['menuFilesDMA']; $targetb = str_replace(' ','_', $targetb); mysql_connect("localhost","abvbnr","cbnvbnd"); mysql_select_db("real") or die("Unable to select database"); $keywords = preg_split("/[\s,]+/", trim($_POST['keyword'])); $sql = "SELECT * FROM items WHERE country='" . mysql_real_escape_string($targetb) . "' AND type='" . mysql_real_escape_string($_POST['Type']) . "' AND Abstract LIKE '%$keyword%' ORDER BY id DESC"; $result = mysql_query($sql); $num_rows = mysql_num_rows($result); if($num_rows == 0) { echo "No results please try a different <a href=asearch.html>search</a>."; } else { while($row = mysql_fetch_array($result)) { $Country = $row['country']; $Type = $row['type']; $More = $row['id']; $Title = $row['Title']; $Abs = $row['Abstract']; $Auth = $row['name']; echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <tr> <td width=\"14\" height=\"28\" background=\"line_left_corner_top.png\"> </td> <td height=\"28\" colspan=\"4\" background=\"line_top.png\"> </td> <td height=\"28\" background=\"line_right_corner_top.png\"> </td> </tr> <tr> <td width=\"14\" rowspan=\"3\" background=\"line_left.png\"> </td> <td width=\"250\" height=\"21\"><span class=\"Large_Blue\">$Title</span></td> <td width=\"154\" height=\"21\"> </td> <td width=\"14\"> </td> <td width=\"128\" height=\"128\" rowspan=\"3\"><img src=\"/searchthumbs/$Country$Type.png\" width=\"128\" height=\"128\" /></td> <td width=\"14\" rowspan=\"3\" background=\"line_right.png\"> </td> </tr> <tr> <td height=\"86\" colspan=\"2\"><span class=\"Small_Black\">$Abs</span></td> <td width=\"14\"> </td> </tr> <tr> <td width=\"250\" height=\"19\"><span class=\"Large_Blue\">$Auth</span></td> <td width=\"154\" height=\"19\" align=\"left\" valign=\"bottom\"><span class=\"style5\">>></span> <span class=\"style7\"><a href=more.php?id=$More>Read More</a> </span> <span class=\"style5\">>></span></td> <td width=\"14\"> </td> </tr> <tr> <td width=\"14\" height=\"19\" background=\"line_left_corner.png\"> </td> <td height=\"28\" colspan=\"4\" background=\"line_base.png\"> </td> <td width=\"14\" height=\"19\" background=\"line_right_corner.png\"> </td> </tr> <tr> <td width=\"14\" height=\"19\"> </td> <td height=\"28\" colspan=\"4\"> </td> <td width=\"14\" height=\"19\"> </td> </tr> </table>"; } } ?> Can anyone help me? All help is much appreciated, Cheers Colin Quote Link to comment https://forums.phpfreaks.com/topic/70150-solved-help-searching/ Share on other sites More sharing options...
cs1h Posted September 21, 2007 Author Share Posted September 21, 2007 Hi, Just to update my question I tried changing $sql = "SELECT * FROM items WHERE country='" . mysql_real_escape_string($targetb) . "' AND type='" . mysql_real_escape_string($_POST['Type']) . "' AND Abstract LIKE '%$keyword%' ORDER BY id DESC"; To $sql = "SELECT * FROM items WHERE country='" . mysql_real_escape_string($targetb) . "' AND type='" . mysql_real_escape_string($_POST['Type']) . "' AND Abstract LIKE '%$keyword%' OR town LIKE '%$keyword%' ORDER BY id DESC"; But this did not work. Does anyone know if I am on the right track with this? Thanks Colin Quote Link to comment https://forums.phpfreaks.com/topic/70150-solved-help-searching/#findComment-352299 Share on other sites More sharing options...
Fadion Posted September 21, 2007 Share Posted September 21, 2007 try changing this: $result = mysql_query($sql); to: $result = mysql_query($sql) or die(mysql_error()); and see what error are u getting, if any. Quote Link to comment https://forums.phpfreaks.com/topic/70150-solved-help-searching/#findComment-352425 Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 I'd go with this... $sql = "SELECT * FROM items WHERE country='" . mysql_real_escape_string($targetb) . "' AND type='" . mysql_real_escape_string($_POST['Type']) . "' AND (Abstract LIKE '%$keyword%' OR town LIKE '%$keyword%') ORDER BY id DESC"; Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70150-solved-help-searching/#findComment-352426 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.