Auto complete text box with PHP, jQuery and MySql

Create table 'tag' in your database and just paste this mysql query.
  1. CREATE TABLE `tag` (  
  2.   `id` int(20) NOT NULL auto_increment,  
  3.   `namevarchar(50) NOT NULL,  
  4.   PRIMARY KEY  (`id`)  
  5. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;  
  6.   
  7. INSERT INTO `tag` (`id`, `name`) VALUES  
  8. (1, 'php'),  
  9. (2, 'php frameword'),  
  10. (3, 'php tutorial'),  
  11. (4, 'jquery'),  
  12. (5, 'ajax'),  
  13. (6, 'mysql'),  
  14. (7, 'wordpress'),  
  15. (8, 'wordpress theme'),  
  16. (9, 'xml');  
Create 'index.php' file in your folder and paste the code below.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6.         <title>Auto Complete Input box</title>  
  7.         <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />  
  8.         <script type="text/javascript" src="jquery.js"></script>  
  9.         <script type="text/javascript" src="jquery.autocomplete.js"></script>  
  10.         <script>    
  11.  $(document).ready(function()  
  12.  {    
  13.   $("#tag").autocomplete("autocomplete.php",  
  14.   {    
  15.         selectFirst: true    
  16.   });    
  17.  });    
  18. </script>  
  19.     </head>  
  20.     <body>  
  21.         <label>Tag:</label>  
  22.         <input name="tag" type="text" id="tag" size="20"/>  
  23.     </body>  
  24. </html>   
Create 'autocomplete.php' file in same folder and paste the code below.
  1. <?php  
  2.  $q=$_GET['q'];  
  3.  $my_data=mysql_real_escape_string($q);  
  4.  $mysqli=mysqli_connect('localhost','username','password','databasename'or die("Database Error");  
  5.  $sql="SELECT name FROM tag WHERE name LIKE '%$my_data%' ORDER BY name";  
  6.  $result = mysqli_query($mysqli,$sqlor die(mysqli_error());  
  7.   
  8.  if($result)  
  9.  {  
  10.   while($row=mysqli_fetch_array($result))  
  11.   {  
  12.    echo $row['name']."\n";  
  13.   }  
  14.  }  
  15. ?>