Jump to content

[SOLVED] How to detect empty SQL query result?


virtuexru

Recommended Posts

I have a full text search. Basically I want to detect "No results" and echo a certain string. How should I go about doing this? This is the code I have currently:

 

	if(isset($_GET['hyb']))
	  {
		$keyword = mysql_real_escape_string($_GET['hyb']);
		$query   = "SELECT * FROM ***** WHERE _subModel LIKE $keyword";
	  }
	if(isset($_GET['hc']))
	  {
		$keyword = mysql_real_escape_string($_GET['hc']);
		$query   = "SELECT * FROM ***** WHERE _make LIKE \"%$keyword%\"";
	  }		  
	if(!empty($keyword))
	  {		  
		$query  = "SELECT *, MATCH(_make, _model, _desc, _year, _subModel) AGAINST('$keyword') AS score
					 FROM *****
						WHERE MATCH(_make, _model, _desc, _year, _subModel) AGAINST('$keyword') 
							ORDER BY score DESC";

		echo "<h3>Search Results for: <font color='red'>".$keyword."</font></h3>";
	  }
	if(empty($keyword))
		  {
		echo "<h3>Please enter a search term.</h3>";
		$error = 1;
	  }
			   				   
        $result = mysql_query($query) or die(mysql_error());

	if($error != 1)
	{			
        while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
	$content = nl2br(strip_tags(substr($row['_desc'], 0, 250)));
	$content = $content;
	$score   = $row['score'];

	if($score > 1.5)
	{
	echo "
                Results go here";
                }

etc..

Check the number of records returned before you process the results:

<?php
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0)
{
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  {
    /* ... */
  }
}
else
{
  echo "No results";
}
?>

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.