Advertisement
Sabbir-bin

Untitled

Dec 13th, 2021
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1.  
  2.  
  3. <?php
  4. $servername = "localhost";
  5. $username = "root";
  6. $password = "";
  7. $database = "webdemo";
  8.  
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $database);
  11.  
  12. // Check connection
  13. if ($conn->connect_error) {
  14.   die("Connection failed: " . $conn->connect_error);
  15. }
  16. // echo "Connected successfully";
  17. ?>
  18.  
  19. <!DOCTYPE html>
  20. <html>
  21. <head>
  22.     <title>Search results</title>
  23. </head>
  24. <body>
  25. <?php
  26.     $query = $_GET['query'];
  27.    
  28.     $min_length = 3;
  29.    
  30.     if(strlen($query) >= $min_length){
  31.        
  32.         $query = htmlspecialchars($query);
  33.         $query = $conn -> real_escape_string($query);
  34.         $sql = "SELECT * FROM profile
  35.        WHERE (`country` LIKE '%".$query."%') OR (`country` LIKE '%".$query."%')";
  36.         $raw_results = $conn -> query($sql) or die(mysql_error());
  37.  
  38.         if($raw_results->num_rows > 0){ // if one or more rows are returned do following
  39.             while($row = $raw_results -> fetch_assoc()) {
  40.                 printf("Name: %s \tCountry: %s<br />",
  41.                    $row["Name"],
  42.                    $row["Country"]);              
  43.              }
  44.              mysqli_free_result($raw_results);
  45.            
  46.         }
  47.         else{
  48.             echo "No results";
  49.         }
  50.     }
  51.     else{
  52.         echo "Minimum length is ".$min_length;
  53.     }
  54.          $conn -> close();
  55. ?>
  56. </body>
  57. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement