Created
January 23, 2019 20:08
-
-
Save gonzalezgarciacristian/0d4ec75a25e51237e4e7f1c1810457cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="es"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Raspberry Pi desde cero</title> | |
</head> | |
<body> | |
<h1>Prueba de Apache, PHP y MariaDB</h1> | |
<div> | |
<h2>Lista de la compra</h2> | |
<table> | |
<tr> | |
<th>ID</th> | |
<th>Nombre</th> | |
<th>Cantidad</th> | |
</tr> | |
<?php | |
$db = new Database(); | |
$result = $db->query("SELECT id, name, quantity FROM products"); | |
while($row = $result->fetch_array()){ | |
echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>"; | |
} | |
// Liberar los resultados | |
$result->free(); | |
// Cerrar conexión | |
$db->close(); | |
?> | |
</table> | |
</div> | |
</body> | |
</html> | |
<?php | |
class Database{ | |
// Dirección o IP de la base de datos | |
private $host = "localhost"; | |
// Puerto de la base de datos | |
private $port = "3306"; | |
// Usuario y contraseña de la base de datos | |
private $user = "pi"; | |
private $password = "superpass"; // Nunca la subáis en un commit o en un ejemplo | |
// Nombre de la base de datos | |
private $bbddName ="rpi"; | |
// Nombre de la tabla | |
private $table = "list"; | |
public function __construct() { | |
$this->connect(); | |
} | |
private function connect(){ | |
$this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->bbddName); | |
return $this->mysqli; | |
} | |
public function query($sql){ | |
$result = $this->mysqli->query($sql); | |
return $result; | |
} | |
public function close(){ | |
$this->close(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment