mikejs Posted January 15, 2010 Share Posted January 15, 2010 Hi I have created a search page using ajax and mysql to display the search results the search works fine, however if I press search without any input it displays the whole database all results ?? driving me crazy any help much appretiated this is my search.php :- if($type == "results") { //run query where search value is like table fields $sql = "SELECT * FROM articles WHERE ( "; $sql .= " UPPER(title) LIKE '%" . $query . "%' OR "; $sql .= " UPPER(category) LIKE '%" . $query . "%' OR "; $sql .= " UPPER(body) LIKE '%" . $query . "%' "; $sql .= ") order by category"; $q = (mysql_query($sql)); // echo $q; $num_rows = mysql_num_rows($q); ///check no of rows if($num_rows>0){ echo " <b>Your search for the word<font color=blue> $query </font>returned $num_rows results: -\n</b>"; echo "<div id=\"scrollWrapper\">"; echo "<div id=\"scroll\">"; // while there are rows echo output to produce page of results while($d = mysql_fetch_array($q)){ $id = $d['id']; $title = $d['title']; $category = $d['category']; $body = $d['body']; $ref = $d['ref']; /// produces page of results echo "<div class =\"results\"><p><strong>Category: </strong>" . $category . "<br>";// using a div to be able to manipulate the output in css echo '<strong>Title: </strong>' . $title . '<br>'; echo '<br>'; echo '' . $body . '<br>'; echo '<br>'; echo '<b>Reference: ' . $ref . '</b><hr>'; echo "</div>"; //end while loop }else { echo"<p>Sorry, no search result. Please change keywords and try again.</p>"; }//end if echo "</div>"; echo "</div>"; } mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted January 15, 2010 Share Posted January 15, 2010 You haven't exactly posted the relevant code. I would check if $query is empty and if so, exit. Quote Link to comment 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.