Ver Mensaje Individual
  #7 (permalink)  
Antiguo 17/10/2009, 00:15
littler
 
Fecha de Ingreso: octubre-2009
Ubicación: Caracas - Venezuela
Mensajes: 14
Antigüedad: 15 años, 1 mes
Puntos: 1
Respuesta: Postgres con php bajo windows

Hola M. Karina,

espero que esto te pueda se de utilidad:


11.2 Inserting and Retrieving Data
Connecting to the database is just not enough. In this section you will learn how to communicate with the database, how to create tables, and how to insert and retrieve data.

The first thing to do is to create a new table. Therefore you can write a short PHP script:

<?php
$connstr = "dbname=phpbook user=postgres host=localhost";
$dbh = pg_connect($connstr);
if ($dbh) {echo "connection to phpbook established ...<br>";}

$sql = "CREATE TABLE person (id serial, name text, location text)";
$stat = pg_exec($dbh, $sql);
if ($stat)
{
echo "The table has successfully been created<br>\n";
}
else
{
echo "creating the table failed<br>\n";
}
pg_close($dbh);
?>

After connecting to the database, a string containing the SQL code is generated. In the next step the query is sent to the server. pg_exec returns a status code that can be checked. Two parameters have to be passed to pg_exec: The first parameter is the connection handle and the second parameter contains the SQL statement you want to execute. If the statement has been executed successfully, the following text will be displayed:

connection to phpbook established ...
The table has successfully been created

To see if the table is really in the database, you can use psql and \d:

phpbook=# \d person
Table "person"
Attribute | Type | Modifier
-----------+---------+---------------------------------------------------
id | integer | not null default nextval('"person_id_seq"'::text)
name | text |
location | text |
Index: person_id_key

Now that you have created a table, you can start inserting records. The next example shows how one record can be added to the table:

<?php
$connstr = "dbname=phpbook user=postgres host=localhost";
$dbh = pg_connect($connstr);
if ($dbh) {echo "connection to phpbook established ...<br>";}

$sql = "INSERT INTO person (name, location) VALUES ('Paul', 'Vienna')";
$stat = pg_exec($dbh, $sql);
if ($stat)
{
echo "The record has been inserted successfully<br>\n";
}
else
{
echo "creating the table failed<br>\n";
}
pg_close($dbh);
?>

As you can see, adding records to the table works the same way as adding tables or executing any other SQL statement. In this regard, PHP is flexible and easy to use.

The text displayed by PHP is not surprising:

connection to phpbook established ...
The record has been inserted successfully

Now that the table contains some data, you can retrieve some records. Retrieving data is slightly more complex than inserting data or making other changes. To retrieve data, you must execute the query first. In the next step, the number of records in the result can be computed. Finally, the data can be retrieved using a simple loop as shown in the next listing:

<?php
$connstr = "dbname=phpbook user=postgres host=localhost";
$dbh = pg_connect($connstr);
if ($dbh) {echo "connection to phpbook established ...<br>";}

$sql = "SELECT id, name, location FROM person";
$stat = pg_exec($dbh, $sql);
if ($stat)
{
$rows = pg_numrows($stat);
echo "$rows lines returned by PostgreSQL<br>\n";
}
else
{
echo "an error has occurred while processing ($sql)<br>\n";
}
pg_close($dbh);
?>





Saludos,


Robert.