2

I have a PHP search. Currently, when no results are displayed the script shows and error. How can I make it display a message like "No Results found" when nothing is returned?

<?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()
?>
3
  • 1
    i hope this is typo: mysql_close() missing semi colon Commented Mar 15, 2016 at 7:52
  • 1
    There is no if statement condition and you are using else. Commented Mar 15, 2016 at 7:53
  • 1) stop using deprecated extension,2) print_r($_POST), 3) ELSE must need his brother IF Commented Mar 15, 2016 at 7:56

6 Answers 6

2

Few Suggestions:

  • Stop using mysql_* extension, its deprecated and close in PHP 7, use mysqli_* or PDO.
  • try to debug print_r($_POST) if need.
  • You must need to use IF for ELSE condition.
  • When you are using HTML Form input directly in your Query Statement, you need to learn mysqli::real_escape_string

Here is the complete example of your code by using MYSQLi Object Oriented:

Example:

<?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();

?>
Sign up to request clarification or add additional context in comments.

Comments

1

WARNING STOP USING mysql_* use mysqli_* or PDO
Your code is wrong, you need an if to use 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();

1 Comment

thanks for the code, it really working! but when i try not to search the name, it display "no result were found".
1

Please try this

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();

Comments

1

please use if for else statement

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

Comments

1

Get the count of records firstly to display the records. Also add the code snippet in a try catch block to track the exceptions without displaying them on 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.");
}
?>

Comments

1

There is a syntax error in your code. Else has no if

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

Moreover, checkout sql injection you are passing in line parameter which can lead to sql injection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.