Jump to content

Add "No results Found" javascript


phpYESvice

Recommended Posts

Hello Php Friends,

I have this search code, it wents well but i want to add and echo or maybe a script that woul tell "NO RESULTS FOUND" in case the search does not found data in the database.
Please anyone can help.

include 'Connect.php';
 //Connect to MySQL Server
mysql_connect($host, $dbusername, $dbpassword);
 //Select Database
mysql_select_db($dbname) or die(mysql_error());
 // Retrieve data from Query 

 $first_name = $_GET['first_name'];
 // Escape User Input to help prevent SQL Injection
$first_name = mysql_real_escape_string($first_name);

$query = "SELECT * FROM student_information WHERE first_name = '$first_name'";
$qry_result = mysql_query($query) or die(mysql_error());
  
$display_string = "<table border='1' cellpadding='10' cellspacing='1' align='center'>";
$display_string .= "<tr align='center'>";
$display_string .= "<th>LR Number</th>";
$display_string .= "<th>F i r s t   N a m e</th>";
$display_string .= "<th>L a s t   N a m e</th>";
$display_string .= "<th>Grade</th>";
$display_string .= "<th>Section</th>";    
$display_string .= "<th>View</th>";  
$display_string .= "<th>Update</th>";
$display_string .= "<th>Delete</th>";
$display_string .= "</tr>";
// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
 $display_string .= "<tr>";
 $display_string .= "<td>$row[LRN]</td>";
 $display_string .= "<td>$row[first_name]</td>";
 $display_string .= "<td>$row[last_name]</td>";
 $display_string .= "<td>$row[grade]</td>";
 $display_string .= "<td>$row[section]</td>";
 $display_string .= "<td><a href='View_Profile.php?id=$student_id'>View</a> </td>";
 $display_string .= "<td><a href='Admin_Edit_Student_Info.php?id=$student_id'>Update</a></td>";
 $display_string .= "<td><a href='Delete.php?id=$student_id'>Delete</a></td>";
}
$display_string .= "</table>";
echo $display_string;
?>
Kindly tell me what codes i am missing and where exactly to put it.
Link to comment
https://forums.phpfreaks.com/topic/288439-add-no-results-found-javascript/
Share on other sites


<?php
 
//Connect to MySQL Server
include 'Connect.php';
mysql_connect($host, $dbusername, $dbpassword);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
 
// Escape User Input to help prevent SQL Injection
$first_name = mysql_real_escape_string(trim($_GET['first_name']));
// Retrieve data from Query  
$query = "SELECT id, LRN, first_name, last_name, grade, section, 
          FROM student_information
          WHERE first_name LIKE '%{$first_name}%'";
$result = mysql_query($query) or die(mysql_error());
 
//Generate the output
$searchResults = '';
if(!mysql_num_rows($result))
{
    $searchResults = "<tr><td colspan='8'>No results found</td></tr>\n";
}
else
{
    // Insert a new row in the table for each person returned
    while($row = mysql_fetch_array($result))
    {
        $student_id = $row[id]; // - ??? Not sure if this is the correct field name
        $searchResults .= "<tr>\n";
        $searchResults .= "  <td>{$row['LRN']}</td>\n";
        $searchResults .= "  <td>{$row['first_name']}</td>\n";
        $searchResults .= "  <td>{$row['last_name']}</td>\n";
        $searchResults .= "  <td>{$row['grade']}</td>\n";
        $searchResults .= "  <td>{$row['section']}</td>\n";
        $searchResults .= "  <td><a href='View_Profile.php?id={$student_id}'>View</a> </td>\n";
        $searchResults .= "  <td><a href='Admin_Edit_Student_Info.php?id={$student_id}'>Update</a></td>\n";
        $searchResults .= "  <td><a href='Delete.php?id={$student_id}'>Delete</a></td>\n";
        $searchResults .= "</tr>\n";
    }
}
 
?>
<html>
<head></head>
<body>
 
<table border='1' cellpadding='10' cellspacing='1' align='center'>
    <tr align='center'>
        <th>LR Number</th>
        <th>F i r s t   N a m e</th>
        <th>L a s t   N a m e</th>
        <th>Grade</th>
        <th>Section</th>
        <th>View</th>
        <th>Update</th>
        <th>Delete</th>
    </tr>
    <?php echo $searchResults; ?>
</table>
 
</body>
</html>

If you look at the code Psycho provided to you, notice how he / she used

if(!mysql_num_rows($result)) {

to see how many rows have been returned by the query.

 

If you have 0 returned rows, there are no results. It's a simple IF statement :)

Thank you Clarkey for helping me analyzing and understanding the codes. I miss that line though.

 

Hello Psycho,

 

 

 

$query = "SELECT id, LRN, first_name, last_name, grade, section, 
          FROM student_information
          WHERE first_name LIKE '%{$first_name}%'"
;

i just replace this with 

$query = "SELECT * 
          FROM student_information
          WHERE first_name LIKE '%{$first_name}%'";

and ALL IS WELL...

 

thank you so much for a big help. This forum has been solved.

You should not use "SELECT *" in almost all instances. It is a bad, lazy habit to get into. You should only select the fields that you intend to use. It makes your queries more efficient, it reduces the potential for defects (especially over time), and it helps to secure your site in the event that data from the query gets "leaked".

hello Psycho,

 

i have followed your advice

 

$query = "SELECT  student_id, LRN, first_name, last_name, grade, section  FROM student_information WHERE first_name LIKE '%{$first_name}%'";

 

i just deleted the comma after the section and it stil work well. 

Your the best!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.