PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 25, 2022

[FIXED] Adding "No Results" message when search returns no results

 January 25, 2022     php, phpmyadmin     No comments   

Issue

I need some help adding a "No Results" message to pop up on the page below the search box if no results are found. Any assistance would be appreciated.

Current Code:

<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="" method="post">  
Search: <input type="text" placeholder="Enter Address" name="term" /><br />  
<br />
<button class="ladda-button btn btn-primary" data-style="zoom-in">Submit
</button>  
</form>  
<?php
    if (!empty($_REQUEST['term'])) 
    {
        $term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM triadlocations WHERE address LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

    while ($row = mysql_fetch_array($r_query))
    {  
    echo '<br /><br /> Location ID: ' .$row['locationid'];  
    echo '<br /> Address: ' .$row['address'];  
    echo '<br /> City: '.$row['city'];  
    echo '<br /> State: '.$row['state'];  
    echo '<br /> Zip: '.$row['zip'];
    }

}
?>
</body>
</html>

Solution

Check the number of results before your while loop. Print the message if there are no results.

if (mysql_num_rows($r_query)) {
    while ($row = mysql_fetch_array($r_query)){  
    ...
    }
} else {
    echo "No Results";
}


Answered By - Erin
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing