Last active
March 16, 2018 15:47
-
-
Save elialejandro/4743436 to your computer and use it in GitHub Desktop.
Demostración de como se realiza una consulta y se muestran los datos usando PDO_MYSQL
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
<?php | |
$options = array( | |
"username" => "usuario", | |
"password" => "password", | |
"host" => "localhost", | |
"dbname" => "base de datos", | |
); | |
// Ejemplo en CONEXIÓN PDO | |
$db = new PDO("mysql:dbname=".$options["dbname"].";host=".$options["host"], $options["username"], $options["password"]); | |
$db->exec("SET NAMES utf8"); | |
// Buscar todos los productos | |
$stmt = $db->prepare('SELECT id, nombre, precio_unitario FROM productos'); | |
$data = $stmt->fetchAll(PDO::FETCH_OBJ); | |
?> | |
<table> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>Nombre</th> | |
<th>Precio Unitario</th> | |
</tr> | |
</head> | |
<tbody> | |
<?php foreach($data as $row) ?> | |
<tr> | |
<td><?php echo $row->id ?></td> | |
<td><?php echo $row->nombre ?></td> | |
<td><?php echo $row->precio_unitario ?></td> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment