Jump to content

[SOLVED] Boolean Search Help


peter_anderson

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/178857-solved-boolean-search-help/
Share on other sites

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.