Advertisement
konyakov

suggestions.php | AJAX+PHP+MySQL Autosuggest (Cyr & Utf-8)

Dec 26th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. <?php
  2.     //plain text header
  3.     header("Content-Type: text/plain; charset=UTF-8");
  4.    
  5.     //database information
  6.     $sDBServer = "DBServer";
  7.     $sDBName = "DBName";
  8.     $sDBUsername = "DBUsername";
  9.     $sDBPassword = "DBPassword";
  10.  
  11.     //include JSON-PHP and instantiate the object
  12.     require_once("JSON.php");
  13.     $oJSON = new JSON();
  14.    
  15.     //get the data that was posted
  16.     $oData = $oJSON->decode($HTTP_RAW_POST_DATA);
  17.     $aSuggestions = array();
  18.  
  19.     //make sure there's text
  20.     if (strlen($oData->text) > 0) {
  21.  
  22.         //create the SQL query string
  23.         $sQuery = "Select Name from ".$oData->requesting." where Name like '".
  24.                   $oData->text."%' order by Name ASC limit 0,".$oData->limit;
  25.              
  26.         //make the database connection
  27.         $oLink = mysql_connect($sDBServer,$sDBUsername,$sDBPassword);
  28.         mysql_query("SET NAMES utf8");
  29.         @mysql_select_db($sDBName) or die("Unable to open database");
  30.        
  31.         if($oResult = mysql_query($sQuery)) {
  32.             while ($aValues = mysql_fetch_array($oResult,MYSQL_ASSOC)) {            
  33.                 array_push($aSuggestions, $aValues['Name']);
  34.             }
  35.         }
  36.    
  37.         mysql_free_result($oResult);
  38.         mysql_close($oLink);
  39.        
  40.     }
  41.    
  42.     echo($oJSON->encode($aSuggestions));
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement