Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 17, 2019 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/bacc905f7fba851082924526b50f2131 to your computer and use it in GitHub Desktop.
Save parzibyte/bacc905f7fba851082924526b50f2131 to your computer and use it in GitHub Desktop.
<?php
/*
CRUD con PostgreSQL y PHP
@author parzibyte [parzibyte.me/blog]
@date 2019-06-17
================================
Este archivo lista todos los
datos de la tabla, pero en un
ciclo usando un cursor, no
a través de un arreglo
(se supone que es más eficiente)
================================
*/
?>
<?php
include_once "base_de_datos.php";
$consulta = "select id, nombre, edad from mascotas";
# Preparar sentencia e indicar que vamos a usar un cursor
$sentencia = $base_de_datos->prepare($consulta, [
PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL,
]);
# Ejecutar sin parámetros
$sentencia->execute();
# Abajo iteramos
?>
<!--Recordemos que podemos intercambiar HTML y PHP como queramos-->
<?php include_once "encabezado.php" ?>
<div class="row">
<!-- Aquí pon las col-x necesarias, comienza tu contenido, etcétera -->
<div class="col-12">
<h1>Listar con cursor</h1>
<a href="//parzibyte.me/blog" target="_blank">By Parzibyte</a>
<br>
<table class="table table-bordered">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Edad</th>
<th>Editar</th>
<th>Eliminar</th>
</tr>
</thead>
<tbody>
<!--
Y aquí usamos el ciclo while y fecthObject, el cuerpo
del ciclo queda intacto pero ahora estamos usando
cursores :)
-->
<?php while ($mascota = $sentencia->fetchObject()){ ?>
<tr>
<td><?php echo $mascota->id ?></td>
<td><?php echo $mascota->nombre ?></td>
<td><?php echo $mascota->edad ?></td>
<td><a class="btn btn-warning" href="<?php echo "editar.php?id=" . $mascota->id?>">Editar 📝</a></td>
<td><a class="btn btn-danger" href="<?php echo "eliminar.php?id=" . $mascota->id?>">Eliminar 🗑️</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php include_once "pie.php" ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment