Jump to content

[SOLVED] php search query returning boolian value


neogemima

Recommended Posts

Hello,

 

So I am querying my database with php to search by keyword in several different fields (i.e. someone can put in a word and it searches the title of the post, the content, location, and the company name for a match).  As far as I can tell my query is correct, but it is returning false for some reason.

 

I am pretty new to php, I am hoping I have just made a simple mistake.  Here is my code:

 

<?php
//begin recent news doc

$region=$_POST['region'];
$query=$_POST['query'];
$type=$_POST['type'];

$query=trim($query);

if(!get_magic_quotes_gpc()){
$region = addslashes($region);
$query = addslashes($query);
$type = addslashes($type);
}

@ $db = mysqli_connect(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx);

if (mysqli_connect_errno($db)) {
	echo 'Error: Could not connect to database.  Please try again later.';
}

mysqli_select_db($db, 'xxxxxxxxxx');


	$sql = "SELECT id, date, type, region, city, company, title, content FROM Jobposting WHERE title like '%$query%' OR company like '%$query%' OR content like '%$query%' OR city like '%$query%' AND region = $region AND type = $type";
	$queryresult = mysqli_query($db, $sql);
	$num_rows = mysqli_num_rows($queryresult);


	if ($num_rows > 0) {

		while ($rowresult = mysqli_fetch_array($queryresult, MYSQLI_ASSOC)){
		$id = $rowresult['id'];

		echo '<a href="jobarticle.php?id='.$id.'">'.$rowresult['date'].'     '.$rowresult['title'].'   <b>'.$rowresult['company'].'</b> <font size="-1">'.$rowresult['region'].'</font></a>';
		echo "<br>";
	}
	}else{
		echo '<font color="#999999">Your search returned no results.</font><br>';
		echo ''.$query.','.$region.','.$type.'';
		}

?>

 

Here, $query is a word they type in, and $region and $type are from drop down menus with values that match exactly the values that could be in their field in the table.

 

Here is the error:

"Search Results:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/content/b/i/o/biotechposting/html/pages/jobs/jobresults.php on line 67

Your search returned no results.

biotech,Western Region,Diagnostics "

 

I have it echo back the three variables on the last line to confirm they actually have a value.  Is there something wrong with my query?

Alright, this is what I expected:

 

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Region AND type = Entry Level' at line 1 "

 

So it appears the issue is in my query, I don't see it though, what are some possible reasons it may return false?  Just not matching the database?  Should I use the "LIKE" statement for all three variables?

Okay, I've gotten it to query the table now.  The new issue that has arisen is that the query is accepting all rows that meet any of the three variable conditions rather than just the ones with the keyword AND the other two conditions met.  Can I use brackets or anything to make sure that all queries returned meet both the region requirement and the type requirement?

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.