Skip to content

Instantly share code, notes, and snippets.

@elialejandro
Last active March 16, 2018 15:47
Show Gist options
  • Save elialejandro/4743436 to your computer and use it in GitHub Desktop.
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
<?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