如何回显“无结果” MYSQL 中找不到搜索结果时的消息?

发布于 2024-12-23 16:56:41 字数 4856 浏览 1 评论 0原文

我的网站上有 mysql 关键字搜索,效果很好。问题是当有人搜索我未添加的关键字时,结果页面上没有显示任何内容。我不想显示任何内容,而是希望显示“未找到结果”消息。有人告诉我必须使用这段代码:

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

但过去几天我一直在尝试在我的代码中实现该代码,但由于某种原因我无法让它工作。如果有人可以向我展示在我的代码中在哪里以及如何正确实现上述代码来解决我的问题,我将不胜感激,提前致谢。

这是我的代码,以便您可以更好地理解我的问题

<?php
function buildNavigation($pageNum_Recordset1,$tot… | ",$max_links=10, $show_page=true)
{
                GLOBAL $maxRows_Recordset1,$totalRows_Recordset…
    $pagesArray = ""; $firstArray = ""; $lastArray = "";
    if($max_links<2)$max_links=2;
    if($pageNum_Recordset1<=$totalPages_R… && $pageNum_Recordset1>=0)
    {
        if ($pageNum_Recordset1 > ceil($max_links/2))
        {
            $fgp = $pageNum_Recordset1 - ceil($max_links/2) > 0 ? $pageNum_Recordset1 - ceil($max_links/2) : 1;
            $egp = $pageNum_Recordset1 + ceil($max_links/2);
            if ($egp >= $totalPages_Recordset1)
            {
                $egp = $totalPages_Recordset1+1;
                $fgp = $totalPages_Recordset1 - ($max_links-1) > 0 ? $totalPages_Recordset1  - ($max_links-1) : 1;
            }
        }
        else {
            $fgp = 0;
            $egp = $totalPages_Recordset1 >= $max_links ? $max_links : $totalPages_Recordset1+1;
        }
        if($totalPages_Recordset1 >= 1) {
            #    ------------------------
            #    Searching for $_GET vars
            #    ------------------------
            $_get_vars = '';            
            if(!empty($_GET) || !empty($HTTP_GET_VARS)){
                $_GET = empty($_GET) ? $HTTP_GET_VARS : $_GET;
                foreach ($_GET as $_get_name => $_get_value) {
                    if ($_get_name != "pageNum_Recordset1") {
                        $_get_vars .= "&$_get_name=$_get_value";
                    }
                }
            }
            $successivo = $pageNum_Recordset1+1;
            $precedente = $pageNum_Recordset1-1;
            $firstArray = ($pageNum_Recordset1 > 0) ? "<a href=\"$_SERVER[PHP_SELF]?pageNum_Record… :  "$prev_Recordset1";
            # ----------------------
            # page numbers
            # ----------------------
            for($a = $fgp+1; $a <= $egp; $a++){
                $theNext = $a-1;
                if($show_page)
                {
                    $textLink = $a;
                } else {
                    $min_l = (($a-1)*$maxRows_Recordset1) + 1;
                    $max_l = ($a*$maxRows_Recordset1 >= $totalRows_Recordset1) ? $totalRows_Recordset1 : ($a*$maxRows_Recordset1);
                    $textLink = "$min_l - $max_l";
                }
                $_ss_k = floor($theNext/26);
                if ($theNext != $pageNum_Recordset1)
                {
                    $pagesArray .= "<a href=\"$_SERVER[PHP_SELF]?pageNum_Record…
                    $pagesArray .= "$textLink</a>" . ($theNext < $egp-1 ? $separator : "");
                } else {
                    $pagesArray .= "$textLink"  . ($theNext < $egp-1 ? $separator : "");
                }
            }
            $theNext = $pageNum_Recordset1+1;
            $offset_end = $totalPages_Recordset1;
            $lastArray = ($pageNum_Recordset1 < $totalPages_Recordset1) ?  "<a href=\"$_SERVER[PHP_SELF]?pageNum_Record… : "$next_Recordset1";
        }
    }
    return array($firstArray,$pagesArray,$lastArray…
}
?>
<?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_strin… ? 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'];
}

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 to solve my problem, thanks in advance.

Here's my code so you can better understand my problem

<?php
function buildNavigation($pageNum_Recordset1,$tot… | ",$max_links=10, $show_page=true)
{
                GLOBAL $maxRows_Recordset1,$totalRows_Recordset…
    $pagesArray = ""; $firstArray = ""; $lastArray = "";
    if($max_links<2)$max_links=2;
    if($pageNum_Recordset1<=$totalPages_R… && $pageNum_Recordset1>=0)
    {
        if ($pageNum_Recordset1 > ceil($max_links/2))
        {
            $fgp = $pageNum_Recordset1 - ceil($max_links/2) > 0 ? $pageNum_Recordset1 - ceil($max_links/2) : 1;
            $egp = $pageNum_Recordset1 + ceil($max_links/2);
            if ($egp >= $totalPages_Recordset1)
            {
                $egp = $totalPages_Recordset1+1;
                $fgp = $totalPages_Recordset1 - ($max_links-1) > 0 ? $totalPages_Recordset1  - ($max_links-1) : 1;
            }
        }
        else {
            $fgp = 0;
            $egp = $totalPages_Recordset1 >= $max_links ? $max_links : $totalPages_Recordset1+1;
        }
        if($totalPages_Recordset1 >= 1) {
            #    ------------------------
            #    Searching for $_GET vars
            #    ------------------------
            $_get_vars = '';            
            if(!empty($_GET) || !empty($HTTP_GET_VARS)){
                $_GET = empty($_GET) ? $HTTP_GET_VARS : $_GET;
                foreach ($_GET as $_get_name => $_get_value) {
                    if ($_get_name != "pageNum_Recordset1") {
                        $_get_vars .= "&$_get_name=$_get_value";
                    }
                }
            }
            $successivo = $pageNum_Recordset1+1;
            $precedente = $pageNum_Recordset1-1;
            $firstArray = ($pageNum_Recordset1 > 0) ? "<a href=\"$_SERVER[PHP_SELF]?pageNum_Record… :  "$prev_Recordset1";
            # ----------------------
            # page numbers
            # ----------------------
            for($a = $fgp+1; $a <= $egp; $a++){
                $theNext = $a-1;
                if($show_page)
                {
                    $textLink = $a;
                } else {
                    $min_l = (($a-1)*$maxRows_Recordset1) + 1;
                    $max_l = ($a*$maxRows_Recordset1 >= $totalRows_Recordset1) ? $totalRows_Recordset1 : ($a*$maxRows_Recordset1);
                    $textLink = "$min_l - $max_l";
                }
                $_ss_k = floor($theNext/26);
                if ($theNext != $pageNum_Recordset1)
                {
                    $pagesArray .= "<a href=\"$_SERVER[PHP_SELF]?pageNum_Record…
                    $pagesArray .= "$textLink</a>" . ($theNext < $egp-1 ? $separator : "");
                } else {
                    $pagesArray .= "$textLink"  . ($theNext < $egp-1 ? $separator : "");
                }
            }
            $theNext = $pageNum_Recordset1+1;
            $offset_end = $totalPages_Recordset1;
            $lastArray = ($pageNum_Recordset1 < $totalPages_Recordset1) ?  "<a href=\"$_SERVER[PHP_SELF]?pageNum_Record… : "$next_Recordset1";
        }
    }
    return array($firstArray,$pagesArray,$lastArray…
}
?>
<?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_strin… ? 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'];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

|煩躁 2024-12-30 16:56:41

由于您使用的是 Dreamweaver,因此只需添加“如果记录集为空则显示区域”命令,并在其中添加“未找到搜索结果”消息。

Because you are using Dreamweaver, just add a Show Region if Recordset Empty command and put the "No search results found" message in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文