Advertisement
Guest User

consulta_preparada

a guest
Nov 23rd, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.74 KB | None | 0 0
  1.  
  2.  
  3. <?php
  4.  
  5. $host = "localhost";
  6. $user = "root";
  7. $pass = "";
  8. $bd = "tienda";
  9. $clientes = array();
  10.  
  11. $con = @new mysqli($host,$user,$pass,$bd);
  12.  
  13. if($con->connect_errno){
  14.  
  15.     die("Error al conectar a la BD".$con->connect_errno);
  16. }else {
  17.  
  18.     //echo "Conectado";
  19. }
  20.  
  21. if($stm = $con->prepare("SELECT NombreCliente FROM cliente")) {
  22.  
  23.     $stm->execute();
  24.     $stm->bind_result($nombre);
  25.  
  26.     while ($stm->fetch()) {
  27.  
  28.         $clientes[] = array("Nombre"=>$nombre);
  29.     }
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. foreach ($clientes as $value) {
  39.    
  40.     echo $value["Nombre"]."<br/>";
  41. }
  42.  
  43.  
  44.  
  45.  
  46. ?>
  47.  
  48.  
  49. <script type="text/javascript">
  50.    
  51.  
  52.     var clientes = <?php echo json_encode($clientes);?>;
  53.  
  54.     for (i in clientes) {
  55.         c = clientes[i];
  56.         console.log(c['Nombre']);
  57.  
  58.     }
  59.    
  60.    
  61. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement