peter_anderson Posted October 24, 2009 Share Posted October 24, 2009 I'm trying to add search to my forum script, but it's returning zero queries (despite the search term being entered in atleast one thread) Here's my code: <?php //some code removed public function search() { //Set content $content = '<h2>Search Forums</h2> <form action="index.php?action=searchresults" method="post" name="search"> <p> <strong>Search Term:</strong><br /> <input name="searchterm" type="text" /></p> <p> <input name="Submit" type="submit" value="Search" /></p> </form>'; // Return return $content; } public function searchresults($term) { if($term = '') { return 'No search terms entered! Go back and try again.'; exit(); } # Connect To Database # Attempt Connection $sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']); # Error Checking if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $searchterm = $_POST['searchterm']; // Make Table Searchable $tblquery = "ALTER TABLE posts ADD BOOL(content)"; $result = $sql->query($tblquery); // Build Query $query = "SELECT * FROM posts WHERE MATCH(content) AGAINST('$searchterm' IN BOOLEAN MODE)"; $query = $sql->real_escape_string($query); // Perform Query $result = $sql->query($query); if ($result->num_rows==0){ $content = '<h2>Search Results</h2><p>No search results found. Go back and enter a new search.<br /> Common words (such as the, them, then, has) will be ignored.</p>'; } else { //First Content $content = '<h2>Search Results</h2><table border="1" cellpadding="3" cellspacing="1" style="width: 100%; "> <tbody> <tr> <td style="background-color: #e6e6e6;"> <p> Below are the search results for your recent search.</p> <p> The links below are for the topic where a post contains your search terms. They open in a new window.</p>'; // Search Results while ($row = $result->fetch_assoc()) { $fid = $row['topic']; $query2 = "SELECT * FROM topics WHERE id='$fid'"; $result2 = $sql->query($query2); $row2 = $result2->fetch_assoc(); $content .= '<p> - <a href="index.php?action=showtopic&id='.$row['id'].'">'.$row['title'].'</a></p>'; } } $content .= ' </td> </tr> </tbody> </table>'; // Return return $content; } //some code removed ?> Can anybody help? I've turned error_reporting on, but no errors are shown relating to the search. Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 24, 2009 Share Posted October 24, 2009 What are you trying to accomplish with this? $tblquery = "ALTER TABLE posts ADD BOOL(content)"; This is not a valid MySQL query. And this $query = $sql->real_escape_string($query); effectively breaks your query (it will escape quotes around $searchterm). Do escaping on variables BEFORE you put them in query Quote Link to comment Share on other sites More sharing options...
peter_anderson Posted October 24, 2009 Author Share Posted October 24, 2009 What are you trying to accomplish with this? $tblquery = "ALTER TABLE posts ADD BOOL(content)"; This is not a valid MySQL query. And this $query = $sql->real_escape_string($query); effectively breaks your query (it will escape quotes around $searchterm). Do escaping on variables BEFORE you put them in query Thanks Solved now. 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.