★ wanayoo — archive 1999 http://php.net/manual/es/function.pg-exec.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to PostgreSQL
Quick Reference
PostgreSQL
English version of this pageGerman version of this pageJapanese version of this page
Italian version of this pageFrench version of this pageHungarian version of this page
Spanish version of this page
* pg_Close
* pg_cmdTuples
* pg_Connect
* pg_DBname
* pg_ErrorMessage
* pg_Exec
* pg_Fetch_Array
* pg_Fetch_Object
* pg_Fetch_Row
* pg_FieldIsNull
* pg_FieldName
* pg_FieldNum
* pg_FieldPrtLen
* pg_FieldSize
* pg_FieldType
* pg_FreeResult
* pg_GetLastOid
* pg_Host
* pg_loclose
* pg_locreate
* pg_loopen
* pg_loread
* pg_loreadall
* pg_lounlink
* pg_lowrite
* pg_NumFields
* pg_NumRows
* pg_Options
* pg_pConnect
* pg_Port
* pg_Result
* pg_tty

  Thanks to:
 Chek.com
 easyDNS
 VA Linux Systems

  Related sites:
Apache
MySQL
PostgreSQL
Zend Tech.

  Community:
LinuxFund.org
OSDN
Manual: pg_Exec
View the source code for this pageSearch the site



Previous page
 pg_ErrorMessage
 Updated
Thu, 14 Sep 2000
pg_Fetch_Array 
Next page


pg_Exec

(PHP3 <= 3.0.16, PHP4 )

pg_Exec -- Ejecuta una consulta (query)

Descripción

int pg_exec (int connection, string query)

Devuelve un índice de resultado si se pudo ejecutar la consulta, o false en caso de fallo o si connection no es un índice de conexión válido. Se pueden obtener detalles acerca del error mediante la función pg_ErrorMessage() siempre que conection sea válido. Envia una sentencia SQL a la base de datos PostgreSQL especificada por el índice de conexión. connection debe ser un índice válido devuelto por pg_Connect(). El valor de devuelto por esta función es un índice para ser usado al acceder a los resultados de la consulta desde otras funciones PostgreSQL.

Nota: PHP/FI devuelvía 1 si no es una consulta que tenga que devolver datos (inserts o updates, por ejemplo) y un valor mayor que 1 incluso en el caso de selects que no devolvieron nada. En PHP no se puede contar con ninguna de esas suposiciones.


User Contributed Notes: pg_Exec


bkosse@thecreek.com
19-May-1999 07:54
Make sure that you close any connections you have (or use persistant connections), otherwise your pg_exec() call may fail without any warnings showing up.


jason@familynet.net
10-Dec-1999 01:53
Insert returns an OID. Use the pg_GetLastOid() function to SELECT the last inserted row of your session. To get an automatic serialized id, set up your table like this:

<PRE>
SQL:

CREATE TABLE something (
"itemno" int4 PRIMARY KEY DEFAULT NEXTVAL('serial'),
"name" varchar(80),
);
CREATE SEQUENCE serial INCREMENT 1 START 1;


PHP Example:

$result=pg_exec($conn,"INSERT INTO customer (name) VALUES ('test');");
if(!$result) {
error("Unable to update.");
}
else {
// display
$oid = pg_GetLastOid($result);
if($oid<0) error("bad news");
$result=pg_exec($conn,"SELECT itemno FROM customer WHERE oid = $oid;");
if(!$result) error("worse news");
$arr = pg_fetch_array ($result, 0);
echo "

ItemNo: $arr[0]

";
}
</PRE>



bradleytsi@aol.com
19-Jan-2000 12:02
Also, here is a web site that should help you out:
http://w3.one.net/~jhoffman/sqltut.htm



knight@novell.ohsh.u-szeged.hu
21-Jan-2000 05:05
I suppose pg_GetLastOid() returns a zero value when you want to insert a duplicate key. So checking should be the following:
<pre>if (!pg_GetLastOid($result) ||
(pg_GetLastOid($result) < 0)) {
code_on_error
else {
code_on_success
}
</pre>



jeff@pgsql.com
13-Aug-2000 10:11
few note/tips for everyone out there

1: that getlast oid or whatever it's called. that's slow and painful.. try this.

if your table (ie "users" has a serial type (ie "uid") and you want to find out the most recent one. do this select
SELECT currval('id_users_seq');

2nd tip to see if a INSERT/UPDATE worked
or not and how to suppress it's error message

1: $insert=@pg_exec($dbh,$insertstring)
if ($insert == 0 ) { /* it screwed up */ }

2: the @ infron of pg_exec suppresses the errors.

jeff@pgsql.com



ronabop@php.net
17-Aug-2000 05:04
On pg_getlastoid vs. SELECT currval('some_table_seq');
One or the other may work better, depending on usage. Currval() has a concurrency problem, however. If two users hit a page at close to the same time, _before_ an insert (say, 2-10 milliseconds of eachother), they will both get the same currval... the PHP code may do an increment for you, and then an insert..

This means that they will both try to use the next number for their serial number.

There is a similar issue with nextval(). If two users get one at similar times, but one abandons the session/pageload/whatever, you can wind up with holes in the serial numbers.

Hence, pg_getlastoid.



 About Notes


Previous page
 pg_ErrorMessage
 Updated
Thu, 14 Sep 2000
pg_Fetch_Array 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.