简体   繁体   English

在PHP MySQL搜索中未找到结果时如何显示消息

[英]How to Display a message when no results found in PHP MySQL search

I tried but it doesn't work. 我试过了,但是没有用。 How can I make it display a message like "No Results were found" when nothing is returned? 当什么都没有返回时,如何使它显示类似“未找到结果”的消息?

My PHP script is: 我的PHP脚本是:

<?php
$pnam=$_POST["prodnam"];
if($pnam=="")
{
  echo("Please enter a search…");
  return;
}

include("connection.php");

$SQL="SELECT * FROM results WHERE prodnam='".$pnam."'";
$run=mySQL_query($SQL,$con) or die ("SQL query error"); 
$rec=mysql_fetch_array($run);

echo ("<table align='center' style='
  font-family: Arial, Helvetica, sans-serif;
  color:#ffffff;
  font-size: 15px;
  text-align:center;'>");
echo ("<tr>");
echo ("<td>".$rec["dtlsnam"]."</td>");
echo ("</tr>");
echo ("</table>");
?>

Apart from stating the obvious statement to use PDO or mysqli , here's the function you need: mysql_num_rows() . 除了说明使用PDOmysqli的显而易见的语句外,这是您需要的功能: mysql_num_rows()

So, just do check as follows: 因此,只需执行以下检查:

if( mysql_num_rows($run) == 0 ) exit( "No results" );

Use following code to check if records fetched by your query. 使用以下代码检查查询是否获取了记录。

$rec=mysql_fetch_array($run);
if(mysql_num_rows($run)>0)
{

// messaege here

}

And also reduce the risk of SQL injection by using following code 并通过使用以下代码来降低SQL注入的风险

$pnam = stripslashes($pnam);

use mysql_num_rows for checking how many rows query returns and according to that handle the situation. 使用mysql_num_rows检查查询返回的行数并根据情况进行处理。

 $run=mySQL_query($SQL,$con) or die ("SQL query error");
 if(mysql_num_rows($run)>0) { 
        $rec=mysql_fetch_array($run);



        echo ("<table align='center' style='
            font-family: Arial, Helvetica, sans-serif;color:#ffffff;
            font-size: 15px;
            text-align:center;'>");
        echo ("<tr>");
        echo ("<td>".$rec["dtlsnam"]."</td>");
        echo ("</tr>");
        echo ("</table>");
}
else
{
    echo 'no rows present';
}

You can use mysql_num_rows() to get the number of records returned. 您可以使用mysql_num_rows()获取返回的记录数。

<?php

$num_rows = mysql_num_rows($run);

if($num_rows==0)
{
  echo 'No records found';
}
else
{
 $rec=mysql_fetch_array($run);

  echo ("<table align='center' style='
                font-family: Arial, Helvetica, sans-serif;color:#ffffff;
                font-size: 15px;
                text-align:center;'>");
            echo ("<tr>");
            echo ("<td>".$rec["dtlsnam"]."</td>");
            echo ("</tr>");
            echo ("</table>");

}

?>

I am just switching my queries from mysql_ to PDO as well. 我也只是将查询从mysql_切换到PDO。 Rather than just telling you to change, I will try my best to get it right in PDO... if I mess up, I will learn something. 我会尽力在PDO中做到最好,而不仅仅是告诉您进行更改……如果我搞砸了,我会学到一些东西。 Hopefully, you can take that and build from it and improve what you are doing as I continue to improve my queries. 希望您可以从中获取并加以改进,并在我继续改进查询的同时改善您的工作。 I only have one small app upgraded with a few big ones to go. 我只有一个小应用程序升级,还有几个大应用程序可以升级。 Still in learning phase. 仍处于学习阶段。

I will be honest, I never would have upgraded if I haven't gotten on stack overflow all the time now. 老实说,如果我现在一直没有堆栈溢出,我永远都不会升级。 Every person who uses the mysql_connect gets hit on it. 每个使用mysql_connect的人都会受到打击。 Made me get outside of my little programmer box and notice. 让我走出了我的小程序员盒子,并注意到了。 Thanks guys! 多谢你们!

OK, I am about to mess this up, so don't laugh. 好吧,我要把它弄乱了,所以别笑了。

PS Can't believe no one mentioned the Table in the question. PS不能相信没有人提到问题表。 What happens if more than one product has the same name? 如果多个产品具有相同的名称,该怎么办?

$stmt=$con->prepare("SELECT * FROM results WHERE prodnam=:pnam");  // named variables in prepared
$stmt->bindValue(':pnam', $pnam, PDO::PARAM_STR);  // bind it to the variable
$stmt->execute();
$hit = $stmt->rowCount();  // count them rows

if($hit) {
   while($results = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
      echo $results['dtlsnam'];
      /* I ignored the fact you are using tables because maybe it is a good tabular layout 
* you are planning.  You can figure out the table part.  But best to loop and put the table
*start before the loop and the table end after the loop and just loop the rows.
*/
   }
}

//
// Then in your connection include you want something like this.
//
// put in the values for your variables.
<?php
try {
$con = new PDO("mysql:host=" . $DB_MYSQL_HOST. ";dbname=" . $DB_MYSQL_NAME, $DB_MYSQL_USER,   $DB_MYSQL_PASS);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM