Advertisement
RamRaider

searchCountry.php

Sep 23rd, 2021
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2.     if( isset( $_GET['q'] ) ){
  3.        
  4.         # change this to suit - add your db connection
  5.         # and remove these lines
  6.         chdir('../../');
  7.         set_include_path( sprintf('%s/dbo', getcwd() ) );
  8.         require 'db-conn-details.php';
  9.         require 'mysqli-conn.php';
  10.  
  11.        
  12.        
  13.        
  14.        
  15.         # change this to suit
  16.         $sql='select * from `country` where `name` like ? order by `name`';
  17.        
  18.        
  19.        
  20.        
  21.        
  22.         #do NOT change below
  23.         $name='%' . $_GET['q'] . '%';
  24.        
  25.        
  26.         $stmt=$db->prepare( $sql );
  27.         $stmt->bind_param('s',$name);
  28.         $stmt->execute();
  29.         $res=$stmt->get_result();
  30.        
  31.         $fields=array_column( $res->fetch_fields(), 'name' );
  32.        
  33.        
  34.        
  35.  
  36.         $html='<table>';
  37.         $html.='<tr>';
  38.         foreach( $fields as $field ){
  39.             $html.=sprintf('<th>%s</th>',ucwords($field));
  40.         }
  41.         $html.='</tr>';
  42.        
  43.         while( $rs=$res->fetch_object() ){
  44.             $html.='<tr>';
  45.             foreach( $fields as $field ){
  46.                 $html.=sprintf('<td>%s</td>',$rs->{$field});
  47.             }
  48.             $html.='</tr>';
  49.         }
  50.         $html.='</table>';
  51.        
  52.         $stmt->free_result();
  53.         $stmt->close();
  54.        
  55.         exit( $html );
  56.     }
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement