Ver Mensaje Individual
  #3 (permalink)  
Antiguo 17/10/2012, 14:38
Avatar de satjaen
satjaen
 
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 8 meses
Puntos: 10
Respuesta: Php para Autocompletar

Maycol estoy utilizando este pero me da error de:

Código Javascript:
Ver original
  1. Fatal error: Class 'mysqli' not found

Código PHP:
Ver original
  1. <?php
  2.    
  3.     // PHP5 Implementation - uses MySQLi.
  4.     // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
  5.     $db = new mysqli('localhost, 'root' ,'root', 'xxxx');
  6.     if ($mysqli->connect_error) {
  7.    die('Error de Conexión (' . $mysqli->connect_errno . ') '
  8.            . $mysqli->connect_error);
  9. }
  10.  
  11. /*
  12. * Use esto en lugar de $connect_error si necesita asegurarse
  13. * de la compatibilidad con versiones de PHP anteriores a 5.2.9 y 5.3.0.
  14. */
  15. if (mysqli_connect_error()) {
  16.    die('Error de Conexión (' . mysqli_connect_errno() . ') '
  17.            . mysqli_connect_error());
  18. }
  19.  
  20. echo 'Éxito... ' . $mysqli->host_info . "\n";
  21.    
  22.     if(!$db) {
  23.         // Show error if we cannot connect.
  24.         echo 'ERROR: Could not connect to the database.';
  25.     } else {
  26.         // Is there a posted query string?
  27.         if(isset($_POST['queryString'])) {
  28.             $queryString = $db->real_escape_string($_POST['queryString']);
  29.            
  30.             // Is the string length greater than 0?
  31.            
  32.             if(strlen($queryString) >0) {
  33.                 // Run the query: We use LIKE '$queryString%'
  34.                 // The percentage sign is a wild-card, in my example of countries it works like this...
  35.                 // $queryString = 'Uni';
  36.                 // Returned data = 'United States, United Kindom';
  37.                
  38.                 // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
  39.                 // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
  40.                
  41.                 $query = $db->query("SELECT apellidos FROM usuarios WHERE apellidos LIKE '$queryString%' LIMIT 10");
  42.                 if($query) {
  43.                     // While there are results loop through them - fetching an Object (i like PHP5 btw!).
  44.                     while ($result = $query ->fetch_object()) {
  45.                         // Format the results, im using <li> for the list, you can change it.
  46.                         // The onClick function fills the textbox with the result.
  47.                        
  48.                         // YOU MUST CHANGE: $result->value to $result->your_colum
  49.                         echo '<li onClick="fill(\''.$result->value.'\');">'.$result->value.'</li>';
  50.                     }
  51.                 } else {
  52.                     echo 'ERROR: There was a problem with the query.';
  53.                 }
  54.             } else {
  55.                 // Dont do anything.
  56.             } // There is a queryString.
  57.         } else {
  58.             echo 'There should be no direct access to this script!';
  59.         }
  60.     }
  61. ?>

Un saludo