Jump to content

How to echo "no results" message when no search results are found in MYSQL?


NerdArmy

Recommended Posts

I have a mysql keyword search on my website and it works fine. The problem is when someone searches a keyword I haven't added, nothing shows up on the results page. Instead of nothing showing up I would like to have a no results were found message. I was told I have to use this code:

 

<?php
if (mysql_num_rows($rs_main) == 0) {
echo "No records found.";
}
?>

 

But I've been trying to implement that code within my code for the last couple of days and for some reason I can't get it to work. It would greatly appreciated if someone could show me where and how to properly implement the code above within my code below to solve my problem, thanks in advance.

 

Here's my code below so you can better understand my problem.(this is my entire search results page, excluding some pagination code)

 

<?php require_once('Connections/theconnect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
   }
  return $theValue;
}
}
$maxRows_Recordset1 = 5;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

$colname_Recordset1 = "-1";
if (isset($_GET['textfield'])) {
  $colname_Recordset1 = $_GET['textfield'];
}
mysql_select_db($database_theconnect, $theconnect);
$query_Recordset1 = sprintf("SELECT *  FROM abstract  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL SELECT *  FROM cartoons  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT *  FROM cute   WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT * FROM fashion  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT * FROM forguys  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT *  FROM hiphop  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT *  FROM movies WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT *  FROM other  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT *  FROM pop  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL  SELECT *  FROM quotes  WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL SELECT * FROM rnb WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL SELECT * FROM random WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL SELECT * FROM rock WHERE keyword LIKE %s OR id  LIKE %s  UNION ALL SELECT * FROM sports WHERE keyword LIKE %s OR id  LIKE %s  ", GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"),GetSQLValueString("%" . $colname_Recordset1 . "%", "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $theconnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My.com</title>
<style type="text/css">
body {
margin-top: -3px;
margin-bottom: -3px;
background-image: url(images/bg.png);
background-repeat: repeat;
}
</style>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/xml">
<!--
<oa:widgets>
  <oa:widget wid="2149022" binding="#OAWidget" />
</oa:widgets>
-->
</script>
</head>

<body>
<div id="shadow">
  <table width="910" border="0" align="center">
    <tr>
      <td bgcolor="#FFFFFF"><div id="header">
        
        <form id="form1" name="form1" method="get" action="results.php">
          <div id="headlinksR"><?php require_once('includes/header_nav.php'); ?></div>
          <span class="searchbar">
          <input name="textfield" type="text" class="tfield_position" id="textfield" value="" />
          <input name="button" type="submit" class="search" id="button" value="Search!" />
           </span>
        </form>
        
        <?php require_once('includes/logo.php'); ?>
      </div>
        <div id="bodywrap">
          <div id="content">
            <div id="bannerad"><?php require_once('includes/banner_ad.php'); ?>
</div>
            <div class="Csepbar">Facebook Covers</div>
            <div id="centerhold">
              <div id="adhold"><?php require_once('includes/center_ad.php'); ?></div>
              <div id="fbapi">
                <?php require_once('includes/fb_api.php'); ?>
              </div>
            </div>
            <div class="Csepbar"> Results!</div>
            <div id="covers">
              <table align="center" cellspacing="5">
              
                <?php do { ?>
                  <tr>
                    
                    <td class="text"><?php echo $row_Recordset1['name']; ?></td>
               
                  
                  </tr>
                  <tr>
                    <td><img src="<?php echo $row_Recordset1['image']; ?>" width="500" height="200" /></td>
                  </tr>
                  <tr>
                    <td><a href="view.php?id=<?php echo $row_Recordset1['id']; ?>&image=<?php echo $row_Recordset1['image']; ?>"><img src="images/bar_bg.png" alt="FC" width="500" height="30" border="0" /></a></td>
                  </tr>
                  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
              </table>
            </div>
            <div id="pagination">                    
              <?php 
# variable declaration
$prev_Recordset1 = "« previous";
$next_Recordset1 = "next »";
$separator = " | ";
$max_links = 5;
$pages_navigation_Recordset1 = buildNavigation($pageNum_Recordset1,$totalPages_Recordset1,$prev_Recordset1,$next_Recordset1,$separator,$max_links,true); 

print $pages_navigation_Recordset1[0]; 
?>
            <?php print $pages_navigation_Recordset1[1]; ?> <?php print $pages_navigation_Recordset1[2]; ?></div>
          </div>
          <div id="sidenav">
            <div id="fb_like">
       <?php require_once('includes/fb_like.php'); ?>

            </div>
            <div class="sepbar"> <p class="sepbarT">Categories</div>
  <?php require_once('includes/side_nav.php'); ?>

  <div class="sepbar">
    <p span class="sepbarT">Sponsored By</div>
            <div id="sidenavad"><?php require_once('includes/side_ad.php'); ?>
</div></div></div>
        <div id="footer">
        <?php require_once('includes/footer.php'); ?>

      </div></td>
    </tr>
  </table>
</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

Link to comment
Share on other sites

I combined your answer with an answer I got on stackoverflow and I feel I'm on the right track now. I used this code below:

 

if (mysql_num_rows($Recordset1) > 0) {
   // no results
   echo 'No results found.';
} else {
   do {
    // output
   } while($res = mysql_fetch_assoc($Recordset1));
}

 

The problem is whenever I search a keyword I know is in my db, I get  my results along with a "no results found" message.  (The "no results found" message also appears on the top left of my webpage pushing my entire website down.)  And when I searched a keyword I knew was not in my db, I didn't get a message at all, it showed nothing like before. I played around with the greater, less than, and equal signs and got similar problems. What am I doing wrong?

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.