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..

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.