Guest User

Untitled

a guest
Jan 24th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="es">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Raspberry Pi desde cero</title>
  6. </head>
  7. <body>
  8. <h1>Prueba de Apache, PHP y MariaDB</h1>
  9. <div>
  10. <h2>Lista de la compra</h2>
  11. <table>
  12. <tr>
  13. <th>ID</th>
  14. <th>Nombre</th>
  15. <th>Cantidad</th>
  16. </tr>
  17. <?php
  18. $db = new Database();
  19.  
  20. $result = $db->query("SELECT id, name, quantity FROM products");
  21.  
  22. while($row = $result->fetch_array()){
  23. echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>";
  24. }
  25.  
  26. // Liberar los resultados
  27. $result->free();
  28.  
  29. // Cerrar conexión
  30. $db->close();
  31. ?>
  32. </table>
  33. </div>
  34. </body>
  35. </html>
  36.  
  37. <?php
  38. class Database{
  39. // Dirección o IP de la base de datos
  40. private $host = "localhost";
  41.  
  42. // Puerto de la base de datos
  43. private $port = "3306";
  44.  
  45. // Usuario y contraseña de la base de datos
  46. private $user = "pi";
  47. private $password = "superpass"; // Nunca la subáis en un commit o en un ejemplo
  48.  
  49. // Nombre de la base de datos
  50. private $bbddName ="rpi";
  51.  
  52. // Nombre de la tabla
  53. private $table = "list";
  54.  
  55. public function __construct() {
  56. $this->connect();
  57. }
  58.  
  59. private function connect(){
  60. $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->bbddName);
  61. return $this->mysqli;
  62. }
  63.  
  64. public function query($sql){
  65. $result = $this->mysqli->query($sql);
  66. return $result;
  67. }
  68.  
  69. public function close(){
  70. $this->close();
  71. }
  72. }
  73. ?>
Add Comment
Please, Sign In to add comment