验证显示消息"未找到结果"

时间:2016-03-15 07:49:32

标签: php

我有一个PHP搜索。目前,当没有显示结果时,脚本显示和错误。如何让它显示一条消息,例如"未找到结果"什么都没有回来?

<?php
include("incls/connector.php"); 

$result = mysql_query("SELECT * FROM user WHERE username='$_POST[compName]'");

while($rowval = mysql_fetch_array($result))
    {
        $complainer = $rowval['username'];
        $department = $rowval['department'];
        $phonenumber = $rowval['phone_number'];
    }
        else
    {
        echo 'No Results were found';
    }

    mysql_close()
?>

6 个答案:

答案 0 :(得分:2)

几点建议:

  • 停止使用mysql_ *扩展,在PHP 7中弃用并关闭,使用mysqli_*PDO
  • 如果需要,尝试调试print_r($_POST)
  • 您必须使用IF作为ELSE条件。
  • 当您在查询语句中直接使用HTML表单输入时,您需要学习mysqli::real_escape_string

以下是使用 MYSQLi面向对象的代码的完整示例:

示例:

<?php    
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$yourInput = $conn->real_escape_string($_POST['compName']);
$sql = "SELECT * FROM user WHERE username='$yourInput'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        //your stuff
    }    
} 
else 
{
    echo "no record found";
}
$conn->close();

?>

答案 1 :(得分:1)

请试试这个

include("incls/connector.php"); 

$result = mysql_query("SELECT * FROM user WHERE username='$_POST[compName]'");

$num_rows = mysql_num_rows($result); //Check for total number of rows

if($num_rows){
 while($rowval = mysql_fetch_array($result))
    {
        $complainer = $rowval['username'];
        $department = $rowval['department'];
        $phonenumber = $rowval['phone_number'];
    }   
  }
else { echo 'No Results were found'; }

mysql_close();

答案 2 :(得分:1)

警告停止使用mysql_ *使用mysqli_ *或PDO
您的代码有误,需要if才能使用else

include("incls/connector.php"); 

$result = mysql_query("SELECT * FROM user WHERE username='".$_POST['compName']."'");//quote for post array

$num_rows = mysql_num_rows($result); //Check for total number of rows

if(is_int($num_rows) && $num_rows >0){
 while($rowval = mysql_fetch_array($result))
    {
        $complainer = $rowval['username'];
        $department = $rowval['department'];
        $phonenumber = $rowval['phone_number'];
    }   
  }
else { echo 'No Results were found'; }

mysql_close();

答案 3 :(得分:1)

代码中存在语法错误。 Else没有if

if(count($result)==0){
   echo 'No Results were found';
}

此外,checkout sql injection你传入的行参数可以导致sql注入

答案 4 :(得分:1)

请使用if for else statement

if( $rowval ) {
} else {
   echo "No Rows Found";
}

答案 5 :(得分:1)

首先获取记录数以显示记录。还要在try catch块中添加代码片段以跟踪异常,而不在UI上显示它们。     

try {
$result = mysql_query("SELECT * FROM user WHERE username='$_POST[compName]'");

$count = mysql_num_rows($result); //get the count of rows
if($count > 0)
  {
        while($rowval = mysql_fetch_array($result))
       {
        $complainer = $rowval['username'];
        $department = $rowval['department'];
        $phonenumber = $rowval['phone_number'];
       }
   }    
   else
   {
        echo 'No Results were found';
   }    
    mysql_close();  // Missing semicolon in your code
} catch (e){
   print_r("Error while getting the results.");
}
?>