PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи

Страниц (1): [1]

> Найдено сообщений: 1
Solven Отправлено: 13 Февраля, 2013 - 07:28:29 • Тема: Автокомплит • Форум: JavaScript & VBScript

Ответов: 20
Просмотров: 3882
Все просто, используем функцию mysqli_set_charset с 7 по 10 строки, больше танцевать не нужно:
PHP:
скопировать код в буфер обмена
  1.  
  2. <?
  3.         // PHP5 Implementation - uses MySQLi.
  4.         // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase')
  5.         $mysqli = new mysqli('localhost', 'login', 'password', 'base');
  6.  
  7.                 /* change character set to utf8 */
  8.         if (!$mysqli->set_charset("utf8")) {
  9.         printf("Error loading character set utf8: %s\n", $mysqli->error);
  10. }
  11.          
  12.         if (mysqli_connect_errno()) {
  13.         printf("Подключение к серверу MySQL невозможно. Код ошибки: %s\n", mysqli_connect_error());
  14.         exit;
  15.         } else {
  16.                 // Is there a posted query string?
  17.                 if(isset($_POST['queryString'])) {
  18.  
  19.                         $queryString = $mysqli->real_escape_string($_POST['queryString']);
  20.                        
  21.                         // Is the string length greater than 0?
  22.                        
  23.                         if(strlen($queryString) >0) {
  24.                                 // Run the query: We use LIKE '$queryString%'
  25.                                 // The percentage sign is a wild-card, in my example of countries it works like this...
  26.                                 // $queryString = 'Uni';
  27.                                 // Returned data = 'United States, United Kindom';
  28.                                
  29.                                 // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
  30.                                 // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
  31.                                
  32.                                 $query = $mysqli->query("SELECT yourName FROM yourTable WHERE yourName LIKE '$queryString%' LIMIT 10");
  33.                                  
  34.                                 if($query) {
  35.                                         // While there are results loop through them - fetching an Object (i like PHP5 btw!).
  36.                                         while ($result = $query ->fetch_object()) {
  37.  
  38.                                                 // Format the results, im using <li> for the list, you can change it.
  39.                                                 // The onClick function fills the textbox with the result.
  40.                                                
  41.                                                 // YOU MUST CHANGE: $result->value to $result->your_colum
  42.                                         echo '<li onClick="fill(\''.$result->pname.'\');">'.$result->pname.'</li>';
  43.                                 }
  44.                                 } else {
  45.                                         echo 'ERROR: There was a problem with the query.';
  46.                                 }
  47.                         } else {
  48.                                 // Dont do anything.
  49.                         } // There is a queryString.
  50.                 } else {
  51.                         echo 'There should be no direct access to this script!';
  52.                 }
  53.         }
  54. ?>
  55.  

Страниц (1): [1]
Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB