



(13 opiniones)
Bien, pasemos a PostgreSQL, lo primero es instalarla:
# apt-get install postgresql
Postgres le hará unas preguntas, conteste a todas con un enter. Ahora agregue la siguiente línea al archivo /etc/postgresql/pg_hba.conf:
local postgres all trust
Asigne un password al usuario postgres:
# passwd postgres
Reinicie la base de datos:
# /etc/init.d/postgresql restart
Cambie al usuario postgres:
# su postgres
Cree la base de datos DBTEST e ingrese en ella con psql, (el cliente de PostgreSQL):
$ createdb DBTEST && psql DBTEST
Cree la tabla tb_usuarios y agregue algunos registros:
DBTEST>create table tb_usuarios (id serial, nombre varchar(50), email varchar(30));
DBTEST>INSERT INTO tb_usuarios (id, nombre, email) VALUES (NEXTVAL('tb_usuarios_id_seq'), 'Ian Murduck' , 'ianmur@debian.org');
DBTEST>INSERT INTO tb_usuarios (id, nombre, email) VALUES (NEXTVAL('tb_usuarios_id_seq'), 'Debora Murduck' , 'debmur@debian.org');
DBTEST>INSERT INTO tb_usuarios (id, nombre, email) VALUES (NEXTVAL('tb_usuarios_id_seq'), 'Bruces Perens' , 'bperens@debian.org');
<?php
//Importamos la libreria PEAR
require_once 'DB.php';
//Datos de la BD
$user='postgres';
$pass='mexik004';
$host='localhost';
$db_name='DBTEST';
$conn = "pgsql://$user:$pass@$host/$db_name";
$conn = DB::connect($conn, true);
$action=$_REQUEST['action'];
if ( $action == "agregar" ) {
$nombre=$_POST['nombre'];
$email=$_POST['email'];
$sql="INSERT INTO tb_usuarios (id, nombre, email) VALUES (NEXTVAL('tb_usuarios_id_seq'), '$nombre', '$email')";
// echo $sql . "<br />";
$conn->query($sql);
echo "
<SCRIPT language='JavaScript'>
<!--
alert('¡Usuario agregado!');
document.location.href = 'pgsql.php';
--> </script>
";
}
if ( $action == "borrar" ) {
$id=$_GET['id'];
$sql="DELETE FROM tb_usuarios WHERE id=$id;";
$conn->query($sql);
echo "
<SCRIPT language='JavaScript'>
<!--
alert('¡Registro borrado!');
document.location.href = 'pgsql.php';
-->
</script>
";
}
?>
<html>
<head>
<title> :: PHP PGSQL ::</title>
</head>
<body bgcolor="#f4ffe5">
<?
echo '<h2 align="center">PHP y PostgreSQL</h2>';
echo '<h3 align="center">Lista de usuarios</h3>';
echo '<table align="center" border="1" width="50%">';
echo '<tr><td><b>Nombre</b></td><td><b>Email</b></td><td> </td></tr>';
//Query
$sql="SELECT * FROM tb_usuarios";
$result = $conn->query($sql);
//Checamos que no haya error
if (DB::isError($result)) {
die ($result->getMessage());
}
// Se hace un loop a través del result
$filas = $result->numRows();
for ($i=0; $i < $filas; $i++){
$estaFila = $result->fetchRow();
$id = $estaFila[0];
$nombre = $estaFila[1];
$email = $estaFila[2];
?>
<tr><td><?= $nombre ?></td><td><?= $email ?>
</td><td><a href="pgsql.php?action=borrar&id=<?= $id ?>">Borrar</a></td></tr>
<?
} //Fin del loop
?>
</table>
<br /><br />
<h3 align="center">Agregar usuario</h3>
<form action="pgsql.php" method="post">
<input type="hidden" name="action" value="agregar" />
<table align="center" border="1" width="50%">
<tr>
<td><input type="text" name="nombre" value="Pancho López" /></td>
<td><input type="text" name="email" value="correo@gmail.com" /></td>
<td><input type="submit" value="Agregar" /></td></tr>
</table>
</form>
<?
// Cierro la conexión con Postgresql
$conn->disconnect();
?>
</body>
</html>
Cambie mexik004 por el password del usuario postgres y sálve este archivo con el menú Plugins->FTP->Save to FTP server...:
Guardelo en el directorio /var/www, asigne el nombre pgsql.php. Al salvarlo notará que Jedit.org resalta la sintaxis de acuerdo al tipo de archivo, en este caso PHP. Compruebe con Mozilla que puede ver la tabla, agregar y borrar usuarios.
De esta manera hemos hecho nuestra primera tabla de altas y bajas usando Apache, PostgreSQL y PHP, fue fácil, ¿no? Si algo no sale o tienes una duda deja un comentario abajo.
|