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
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>
Edited by Psycho
Link to comment
Share on other sites

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 :)

Edited by Clarkey
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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