PostgreSQL functionsPostgreSQL
Postgres, developed originally in the UC Berkeley Computer Science
Department, pioneered many of the object-relational concepts now
becoming available in some commercial databases. It provides
SQL92/SQL99 language support, transaction integrity and type
extensibility. PostgreSQL is an open source descendant of this
original Berkeley code.
PostgreSQL database is Open Source product and available without
cost. To use PostgreSQL support, you need PostgreSQL 6.5 or
later. PostgreSQL 7.0 or later to enable all PostgreSQL module
feature. PostgreSQL supports many character encoding including
multibyte character encoding. The current version and more
information about PostgreSQL is available at &url.pgsql;.
In order to enable PostgreSQL support,
is required when you compile
PHP. If shared object module is available, PostgreSQL module may
be loaded using extension
directive in &php.ini; or dl
function. Supported ini directives are described in
php.ini-dist which comes with source distribution.
Using the PostgreSQL module with PHP 4.0.6 is not recommended due to
a bug in the notice message handling code. Use 4.1.0 or later.
PostgreSQL function names will be changed in 4.2.0 release to
confirm to current coding standards. Most of new names will have
additional underscores, e.g. pg_lo_open(). Some functions are
renamed to different name for consistency. e.g. pg_exec() to
pg_query(). Older names can be used in 4.2.0 and a few releases
from 4.2.0, but they may be deleted in the future.
Function names changedOld nameNew namepg_execpg_querypg_getlastoidpg_last_oidpg_cmdtuplespg_affected_rowspg_numrowspg_num_rowspg_numfieldspg_num_fieldspg_fieldnamepg_field_namepg_fieldsizepg_field_sizepg_fieldnumpg_field_numpg_fieldptrlenpg_field_ptrlenpg_fieldisnullpg_field_is_nullpg_freeresultpg_free_resultpg_resultpg_fetch_resultpg_loreadallpg_lo_read_allpg_locreatepg_lo_createpg_lounlinkpg_lo_unlinkpg_loopenpg_lo_unlinkpg_loclosepg_lo_closepg_loreadpg_lo_readpg_lowritepg_lo_writepg_loimportpg_lo_importpg_loexportpg_lo_export
The old pg_connect/pg_pconnect
syntax will be deprecated to support asynchronous connections in the
future. Please use a connection string for pg_connect
and pg_pconnect.
Not all functions are supported by all builds. It depends on your
libpq (The PostgreSQL C Client interface) version and how libpq is
compiled. If there is missing function, libpq does not support
the feature required for the function.
It is also important that you use newer libpq than PostgreSQL
Server to be connected. If you use libpq older than PostgreSQL
Server expects, you may have problems.
Since version 6.3 (03/02/1998) PostgreSQL uses unix domain sockets
by default. TCP port will NOT be opened by default. A table is
shown below describing these new connection possibilities. This
socket will be found in /tmp/.s.PGSQL.5432.
This option can be enabled with the '-i' flag to
postmaster and it's meaning is: "listen on
TCP/IP sockets as well as Unix domain sockets".
Postmaster and PHPPostmasterPHPStatuspostmaster &pg_connect("dbname=MyDbName");OKpostmaster -i &pg_connect("dbname=MyDbName");OKpostmaster &pg_connect("host=localhost dbname=MyDbName");
Unable to connect to PostgreSQL server: connectDB() failed:
Is the postmaster running and accepting TCP/IP (with -i)
connection at 'localhost' on port '5432'? in
/path/to/file.php on line 20.
postmaster -i &pg_connect("host=localhost dbname=MyDbName");OK
A connection to PostgreSQL server can be established with the
following value pairs set in the command string: $conn =
pg_connect("host=myHost port=myPort tty=myTTY options=myOptions
dbname=myDB user=myUser password=myPassword ");
The previous syntax of:
$conn = pg_connect ("host", "port", "options", "tty", "dbname")
has been deprecated.
Environmental variables affect PostgreSQL server/client
behavior. For example, PostgreSQL module will lookup PGHOST
environment variable when the hostname is omitted in the connection
string. Supported environment variables are different from version
to version. Refer to PostgreSQL Programmer's Manual (libpq -
Environment Variables) for details.
Make sure you set environment variables for appropriate user. Use
$_ENV or getenv to check
which environment variables are available to the current process.
Setting default parameters
Starting with PostgreSQL 7.1.0, you can store up to 1GB into a
field of type text. In older versions, this was limited to the block
size (default was 8KB, maximum was 32KB, defined at compile time)
To use the large object (lo) interface, it is required to enclose
large object functions within a transaction block. A transaction
block starts with a SQL statement BEGIN and if
the transaction was valid ends with COMMIT or
END. If the transaction fails the transaction
should be closed with ROLLBACK or
ABORT.
Using Large Objects
]]>
You should not close the connection to the PostgreSQL server
before closing the large object.
pg_affected_rowsReturns number of affected records(tuples)Descriptionintpg_affected_rowsresourceresultpg_affected_rows returns the number of tuples
(instances/records/rows) affected by INSERT, UPDATE, and DELETE
queries executed by pg_query. If no tuple is
affected by this function, it will return 0.
pg_affected_rows
]]>
This function used to be called pg_cmdtuples().
See also pg_query and
pg_num_rows.
pg_connectOpen a PostgreSQL connectionDescriptionresourcepg_connectstringconnection_stringpg_connect returns a connection resource
that is needed by other PostgreSQL functions.
pg_connect opens a connection to a
PostgreSQL database specified by the
connection_string. It returns a connection
resource on success. It returns &false; if the connection could
not be made. connection_string should be
a quoted string.
Using pg_connect
]]>
The arguments available for
connection_string includes
host, port,
tty, options,
dbname, user, and
password.
If a second call is made to pg_connect with
the same connection_string, no
new connection will be established, but instead, the connection
resource of the already opened connection will be returned. You
can have multiple connections to the same database if you use
different connection string.
The old syntax with multiple parameters
$conn = pg_connect ("host", "port", "options", "tty", "dbname")
has been deprecated.
See also pg_pconnect,
pg_close, pg_host,
pg_port, pg_tty,
pg_options and pg_dbname.
pg_pconnectOpen a persistent PostgreSQL connectionDescriptionintpg_pconnectstringconnection_stringpg_pconnect opens a connection to a
PostgreSQL database. It returns a connection resource that is
needed by other PostgreSQL functions.
For a description of the connection_string
parameter, see pg_connect.
To enable persistent connection, the pgsql.allow_persistent
&php.ini; directive must be set to "On" (which is the default).
The maximum number of persistent connection can be defined with the pgsql.max_persistent
&php.ini; directive (defaults to -1 for no limit). The total number
of connections can be set with the pgsql.max_links
&php.ini; directive.
pg_close will not close persistent links
generated by pg_pconnect.
See also pg_connect.
pg_connection_busy
Get connection is busy or not
Descriptionboolpg_connection_busyresourceconnectionpg_connection_busy returns &true; if the
connection is busy. If it's is busy, a previous query is still executing.
If pg_get_result is called, it will be blocked.
See also pg_connection_status and
pg_get_resultpg_connection_reset
Reset connection (reconnect)
Descriptionboolpg_connection_resetresourceconnectionpg_connection_reset resets the connection.
It is useful for error recovery. &return.success;
See also pg_connect,
pg_pconnect and
pg_connection_statuspg_connection_status
Get connection status
Descriptionintpg_connection_statusresourceconnectionpg_connection_status returns a connection
status. Possible statuses are PGSQL_CONNECTION_OK
and PGSQL_CONNECTION_BAD.
See also pg_connection_busy.
pg_closeClose a PostgreSQL connectionDescriptionboolpg_closeresourceconnectionpg_close closes the non-persistent
connection to a PostgreSQL database associated with the given
connection resource. &return.success;
Using pg_close is not usually necessary, as
non-persistent open connections are automatically closed at the end
of the script.
If there is open large object resource on the connection, do not
close the connection before closing all large object resources.
pg_copy_from
Copy a table from an array
Descriptionintpg_copy_fromintconnectionstringtable_namearrayrowsstringdelimiterstringnull_aspg_copy_from copies a table from an array.
&return.success;
See also pg_copy_topg_copy_to
Copy a table to an array
Descriptionintpg_copy_tointconnectionstringtable_namestringdelimiterstringnull_aspg_copy_to copies a table to an array. The
resulting array is returned. It returns &false; on failure.
See also pg_copy_frompg_end_copySync with PostgreSQL backendDescriptionboolpg_end_copyresourceconnectionpg_end_copy syncs the PostgreSQL frontend
(usually a web server process) with the PostgreSQL server after
doing a copy operation performed by
pg_put_line. pg_end_copy
must be issued, otherwise the PostgreSQL server may get out of
sync with the frontend and will report an error. &return.success;
For further details and an example, see also
pg_put_line.
pg_put_lineSend a NULL-terminated string to PostgreSQL backendDescriptionboolpg_put_lineresourceconnectionstringdatapg_put_line sends a NULL-terminated string
to the PostgreSQL backend server. This is useful for example for
very high-speed inserting of data into a table, initiated by
starting a PostgreSQL copy-operation. That final NULL-character
is added automatically. &return.success;
The application must explicitly send the two characters "\."
on the last line to indicate to the backend that it has finished
sending its data.
See also pg_end_copy.
High-speed insertion of data into a table
]]>
pg_last_errorGet the last error message string of a connectionDescriptionstringpg_last_errorresourceconnectionpg_last_error returns the last error message
for given connection.
Error messages may be overwritten by internal PostgreSQL(libpq)
function calls. It may not return appropriate error message, if
multiple errors are occured inside a PostgreSQL module function.
Use pg_result_error,
pg_result_status and
pg_connection_status for better error handling.
This function used to be called pg_errormessage().
See also pg_result_error.
pg_last_notice
Returns the last notice message from PostgreSQL server
Descriptionstringpg_last_noticeresourceconnectionpg_last_notice returns the last notice
message from the PostgreSQL server specified by
connection. The PostgreSQL server sends notice
messages in several cases, e.g. if the transactions can't be continued.
With pg_last_notice, you can avoid issuing useless
queries, by checking whether the notice is related to the transaction
or not.
This function is EXPERIMENTAL and it is not fully implemented
yet. pg_last_notice was added in PHP
4.0.6. However, PHP 4.0.6 has problem with notice message
handling. Use of the PostgreSQL module with PHP 4.0.6 is not
recommended even if you are not using
pg_last_notice.
This function is fully implemented in PHP 4.3.0. PHP earlier than
PHP 4.3.0 ignores database connection parameter.
Notice message tracking can be set to optional by setting 1 for
pgsql.ignore_notice ini from PHP 4.3.0.
Notice message logging can be set to optional by setting 0 for
pgsql.log_notice ini from PHP 4.3.0. Unless
pgsql.ignore_notice is set to 0, notice message
cannot be logged.
See also pg_query and
pg_last_error.
pg_last_oidReturns the last object's oidDescriptionintpg_last_oidresourceresultpg_last_oid is used to retrieve the
oid assigned to an inserted tuple (record) if
the result resource is used from the last command sent via
pg_query and was an SQL INSERT. Returns a
positive integer if there was a valid oid. It
returns &false; if an error occurs or the last command sent via
pg_query was not an INSERT or INSERT is
failed.
This function used to be called pg_getlastoid().
See also pg_query.
pg_queryExecute a queryDescriptionresourcepg_queryresourceconnectionstringquerypg_query returns a query result resource if
query could be executed. It returns &false; on failure or if
connection is not a valid connection. Details about the error can
be retrieved using the pg_last_error
function if connection is valid.
pg_last_error sends an SQL statement to
the PostgreSQL database specified by the
connection resource. The
connection must be a valid connection that
was returned by pg_connect or
pg_pconnect. The return value of this
function is an query result resource to be used to access the
results from other PostgreSQL functions such as
pg_fetch_array.
connection is a optional parameter for
pg_query. If
connection is not set, default
connection is used. Default connection is the last connection
made by pg_connect or
pg_pconnect.
Although connection can be omitted, it
is not recommended, since it could be a cause of hard to find
bug in script.
This function used to be called pg_exec().
See also pg_connect,
pg_pconnect,
pg_fetch_array,
pg_fetch_object,
pg_num_rows,
and pg_affected_rows.
pg_fetch_arrayFetch a row as an arrayDescriptionarraypg_fetch_arrayresourceresultintrowintresult_typepg_fetch_array returns an array that
corresponds to the fetched row (tuples/records). It returns
&false;, if there are no more rows.
pg_fetch_array is an extended version of
pg_fetch_row. In addition to storing the
data in the numeric indices (field index) to the result array, it
also stores the data in associative indices (field name) by
default.
row is row (record) number to be
retrieved. First row is 0.
result_type is optional parameter controls
how return value is initialized.
result_type is a constant and can take the
following values: PGSQL_ASSOC, PGSQL_NUM, and PGSQL_BOTH.
pg_fetch_array returns associative array
that has field name as key for PGSQL_ASSOC. field index as key
with PGSQL_NUM and both field name/index as key with
PGSQL_BOTH. Default is PGSQL_BOTH.
result_type was added in PHP 4.0.
pg_fetch_array is NOT significantly
slower than using pg_fetch_row, while it
provides a significant ease of use.
See also pg_fetch_row and
pg_fetch_object and
pg_fetch_result.
PostgreSQL fetch array
]]>
From 4.1.0, row became optional.
pg_fetch_objectFetch a row as an objectDescriptionobjectpg_fetch_objectresourceresultintrowintresult_typepg_fetch_object returns an object with
properties that correspond to the fetched row. It returns &false;
if there are no more rows or error.
pg_fetch_object is similar to
pg_fetch_array, with one difference - an
object is returned, instead of an array. Indirectly, that means
that you can only access the data by the field names, and not by
their offsets (numbers are illegal property names).
result_type is optional parameter controls
how return value is initialized.
result_type is a constant and can take the
following values: PGSQL_ASSOC, PGSQL_NUM, and PGSQL_BOTH.
pg_fetch_array returns associative array
that has field name as key for PGSQL_ASSOC. field index as key
with PGSQL_NUM and both field name/index as key with
PGSQL_BOTH. Default is PGSQL_BOTH.
result_type was added in PHP 4.0.
Speed-wise, the function is identical to
pg_fetch_array, and almost as quick as
pg_fetch_row (the difference is
insignificant).
See also pg_query, pg_fetch_array,
pg_fetch_row and pg_fetch_result.
Postgres fetch object
]]>
From 4.1.0, row became optional.
pg_fetch_resultReturns values from a result resourceDescriptionmixedpg_fetch_resultresourceresultintrow_numbermixedfieldpg_fetch_result returns values from a
result resource returned by
pg_query. row_number
is integer. field is field name(string)
or field index (integer). The row_number
and field specify what cell in the table
of results to return. Row numbering starts from 0. Instead of
naming the field, you may use the field index as an unquoted
number. Field indices start from 0.
PostgreSQL has many built in types and only the basic ones are
directly supported here. All forms of integer,
boolean and void
types are
returned as integer values. All forms of float, and
real types are returned as float values. All other
types, including arrays are returned as strings formatted in the
same default PostgreSQL manner that you would see in the
psql program.
pg_fetch_rowGet a row as an enumerated arrayDescriptionarraypg_fetch_rowresourceresultintrowpg_fetch_row fetches one row of data from
the result associated with the specified
result resource. The row (record) is
returned as an array. Each result column is stored in an array
offset, starting at offset 0.
It returns an array that corresponds to the fetched row, or &false;
if there are no more rows.
See also: pg_query,
pg_fetch_array,
pg_fetch_object and
pg_fetch_result.
Postgres fetch row
";
}
?>
]]>
From 4.1.0, row became optional.
pg_field_is_nullTest if a field is &null;Descriptionintpg_field_is_nullresourceresultintrowmixedfieldpg_field_is_null test if a field is &null; or
not. It returns 1 if the field in the given row is &null;. It
returns 0 if the field in the given row is NOT &null;. Field can
be specified as column index (number) or fieldname (string). Row
numbering starts at 0.
This function used to be called pg_fieldisnull().
pg_field_nameReturns the name of a fieldDescriptionstringpg_field_nameresourceresultintfield_numberpg_field_name returns the name of the field
occupying the given field_number in the
given PostgreSQL result resource. Field
numbering starts from 0.
This function used to be called pg_fieldname().
See also pg_field_num.
pg_field_numReturns the field number of the named fieldDescriptionintpg_field_numresourceresultstringfield_namepg_field_num will return the number of the
column (field) slot that corresponds to the
field_name in the given PostgreSQL
result resource. Field numbering starts
at 0. This function will return -1 on error.
This function used to be called pg_fieldnum().
See also pg_field_name.
pg_field_prtlenReturns the printed lengthDescriptionintpg_field_prtlenresourceresultintrow_numberstringfield_namepg_field_prtlen returns the actual printed
length (number of characters) of a specific value in a PostgreSQL
result. Row numbering starts at 0. This
function will return -1 on an error.
This function used to be called pg_field_prtlen().
See also pg_field_size.
pg_field_size
Returns the internal storage size of the named field
Descriptionintpg_field_sizeresourceresultintfield_numberpg_field_size returns the internal storage
size (in bytes) of the field number in the given PostgreSQL
result. Field numbering starts at 0. A
field size of -1 indicates a variable length field. This function
will return &false; on error.
This function used to be called pg_fieldsize().
See also pg_field_len and pg_field_type.
pg_field_type
Returns the type name for the corresponding field number
Descriptionstringpg_field_typeresourceresultintfield_numberpg_field_type returns a string containing the
type name of the given field_number in the
given PostgreSQL result resource. Field
numbering starts at 0.
This function used to be called pg_field_type().
See also pg_field_len and pg_field_name.
pg_free_resultFree result memoryDescriptionboolpg_free_resultresourceresultpg_free_result only needs to be called if you
are worried about using too much memory while your script is
running. All result memory will automatically be freed when the
script is finished. But, if you are sure you are not going to
need the result data anymore in a script, you may call
pg_free_result with the
result resource as an argument and the
associated result memory will be freed. It returns true on success
and false if an error occurs.
This function used to be called pg_field_len().
See also pg_query.
pg_lo_closeClose a large objectDescriptionboolpg_lo_closeresourcelarge_objectpg_lo_close closes a Large
Object. large_object is a resource for the
large object from pg_lo_open.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_loclose().
See also pg_lo_open,
pg_lo_create and
pg_lo_import.
pg_lo_createCreate a large objectDescriptionintpg_lo_createresourceconnectionpg_lo_create creates a Large
Object and returns the oid of the large
object. connection specifies a valid
database connection opened by pg_connect or
pg_pconnect. PostgreSQL access modes
INV_READ, INV_WRITE, and INV_ARCHIVE are not supported, the
object is created always with both read and write
access. INV_ARCHIVE has been removed from PostgreSQL itself
(version 6.3 and above). It returns large object oid
otherwise. It returns &false;, if an error occurred,
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_locreate().
pg_lo_exportExport a large object to fileDescriptionboolpg_lo_exportintoidstringpathnameresourceconnection
The oid argument specifies oid of the
large object to export and the pathname
argument specifies the pathname of the file. It returns &false; if
an error occurred, &true; otherwise.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_loexport().
See also pg_lo_import.
pg_lo_importImport a large object from fileDescriptionintpg_lo_importstringpathnameresourceconnection
The pathname argument specifies the
pathname of the file to be imported as a large object. It returns
&false; if an error occurred, oid of the just created large
object otherwise.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
¬e.sm.uidcheck;
This function used to be called pg_loimport().
See also pg_lo_export and
pg_lo_open.
pg_lo_openOpen a large objectDescriptionresourcepg_lo_openresourceconnectionintoidstringmodepg_lo_open open a Large Object and
returns large object resource. The resource encapsulates
information about the connection.
oid specifies a valid large object oid and
mode can be either "r", "w", or "rw". It
returns &false; if there is an error.
Do not close the database connection before closing the large
object resource.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_loopen().
See also pg_lo_close and
pg_lo_create.
pg_lo_readRead a large objectDescriptionstringpg_lo_readresourcelarge_objectintlenpg_lo_read reads at most
len bytes from a large object and returns
it as a string. large_object specifies a
valid large object resource andlen
specifies the maximum allowable size of the large object
segment. It returns &false; if there is an error.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_loread().
See also pg_lo_read_all.
pg_lo_seek
Seeks position of large object
Descriptionboolpg_lo_seekresourcelarge_objectintoffsetintwhencepg_lo_seek seeks position of large object
resource. whence is PGSQL_SEEK_SET,
PGSQL_SEEK_CUR or PGSQL_SEEK_END.
See also pg_lo_tell.
pg_lo_tell
Returns current position of large object
Descriptionintpg_lo_tellresourcelarge_objectpg_lo_tell returns current position (offset
from the beginning of large object).
See also pg_lo_seek.
pg_lo_read_all
Read a entire large object and send straight to browser
Descriptionintpg_lo_read_allresourcelarge_objectpg_lo_read_all reads a large object and passes
it straight through to the browser after sending all pending
headers. Mainly intended for sending binary data like images or
sound. It returns number of bytes read. It returns &false;, if an
error occurred.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_loreadall().
See also pg_lo_read.
pg_lo_unlinkDelete a large objectDescriptionboolpg_lo_unlinkresourceconnectionintoidpg_lo_unlink deletes a large object with the
oid. It returns &true; on success,
otherwise returns &false;.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_lo_unlink().
See also pg_lo_create and
pg_lo_import.
pg_lo_writeWrite a large objectDescriptionintpg_lo_writeresourcelarge_objectstringdatapg_lo_write writes at most to a large object
from a variable data and returns the number
of bytes actually written, or &false; in the case of an error.
large_object is a large object resource
from pg_lo_open.
To use the large object (lo) interface, it is necessary to
enclose it within a transaction block.
This function used to be called pg_lo_write().
See also pg_lo_create and
pg_lo_open.
pg_num_fieldsReturns the number of fieldsDescriptionintpg_num_fieldsresourceresultpg_num_fields returns the number of fields
(columns) in a PostgreSQL result. The
argument is a result resource returned by
pg_query. This function will return -1 on
error.
This function used to be called pg_numfields().
See also pg_num_rows and
pg_affected_rows.
pg_num_rowsReturns the number of rowsDescriptionintpg_num_rowsresourceresultpg_num_rows will return the number of rows in
a PostgreSQL result resource.
result is a query result resource returned
by pg_query. This function will return -1 on
error.
Use pg_affected_rows to get number of rows
affected by INSERT, UPDATE and DELETE query.
This function used to be called pg_numrows().
See also pg_num_fields and
pg_affected_rows.
pg_host
Returns the host name associated with the connection
Descriptionstringpg_hostresourceconnectionpg_host returns the host name of the given
PostgreSQL connection resource is
connected to.
See also pg_connect and
pg_pconnect.
pg_port
Return the port number associated with the connection
Descriptionintpg_portresourceconnectionpg_port returns the port number that the
given PostgreSQL connection resource is
connected to.
pg_dbnameGet the database nameDescriptionstringpg_dbnameresourceconnectionpg_dbname returns the name of the database
that the given PostgreSQL connection
resource. It returns &false;, if connection
is not a valid PostgreSQL connection resource.
pg_optionsGet the options associated with the connectionDescriptionstringpg_optionsresourceconnectionpg_options will return a string containing
the options specified on the given PostgreSQL
connection resource.
pg_set_client_encoding
Set the client encoding
Descriptionintpg_set_client_encodingresourceconnectionstringencodingpg_set_client_encoding sets the client
encoding and return 0 if success or -1 if error.
encoding is the client encoding and can be
either : SQL_ASCII, EUC_JP, EUC_CN, EUC_KR, EUC_TW, UNICODE,
MULE_INTERNAL, LATINX (X=1...9), KOI8, WIN, ALT, SJIS, BIG5,
WIN1250. Available encoding depends on your PostgreSQL and libpq
version. Refer to PostgreSQL manual for supported encodings for
your PostgreSQL.
This function requires PHP-4.0.3 or higher and PostgreSQL-7.0 or
higher. Supported encoding depends on PostgreSQL version. Refer
to PostgreSQL manual for details.
The function used to be called
pg_setclientencoding.
See also pg_client_encoding.
pg_client_encoding
Get the client encoding
Descriptionstringpg_client_encodingresourceconnectionpg_client_encoding returns the client
encoding as the string. The returned string should be either :
SQL_ASCII, EUC_JP, EUC_CN, EUC_KR, EUC_TW, UNICODE,
MULE_INTERNAL, LATINX (X=1...9), KOI8, WIN, ALT, SJIS, BIG5,
WIN1250.
This function requires PHP-4.0.3 or higher and PostgreSQL-7.0 or
higher. If libpq is compiled without multibyte encoding support,
pg_set_client_encoding always return
"SQL_ASCII". Supported encoding depends on PostgreSQL
version. Refer to PostgreSQL manual for details to enable
multibyte support and encoding supported.
The function used to be called
pg_clientencoding.
See also pg_set_client_encoding.
pg_tty
Return the tty name associated with the connection
Descriptionstringpg_ttyresourceconnectionpg_tty returns the tty name that server
side debugging output is sent to on the given PostgreSQL
connection resource.
pg_traceEnable tracing a PostgreSQL connectionDescriptionboolpg_tracestringpathnamestringmoderesourceconnectionpg_trace enables tracing of the PostgreSQL
frontend/backend communication to a debugging file specified as
pathname. To fully understand the results,
one needs to be familiar with the internals of PostgreSQL
communication protocol. For those who are not, it can still be
useful for tracing errors in queries sent to the server, you
could do for example grep '^To backend'
trace.log and see what query actually were sent to the
PostgreSQL server. For more information, refer to PostgreSQL
manual.
Filename and mode
are the same as in fopen
(mode defaults to 'w'),
connection specifies the connection to
trace and defaults to the last one opened.
It returns &true; if pathname could be opened
for logging, &false; otherwise.
See also fopen and
pg_untrace.
pg_untraceDisable tracing of a PostgreSQL connectionDescriptionboolpg_untraceresourceconnection
Stop tracing started by pg_trace.
connection specifies the connection that was
traced and defaults to the last one opened.
Returns always &true;.
See also pg_trace.
pg_get_result
Get asynchronous query result
Descriptionresourcepg_get_resultresourceconnectionpg_get_result get result resource from async
query executed by
pg_send_query. pg_send_query
can send multiple queries to PostgreSQL server and
pg_get_result is used to get query result
one by one. It returns result resource. If there is no more
results, it returns &false;.
pg_result_error
Get error message associated with result
Descriptionstringpg_result_errorresourceresultpg_result_error returns error message
associated with result resource. Therefore,
user has better chance to get better error message than
pg_last_error.
See also pg_query,
pg_send_query,
pg_get_result,
pg_last_error and
pg_last_noticepg_result_status
Get status of query result
Descriptionintpg_result_statusresourceresultpg_result_status returns status of result
resource. Possible retun values are PGSQL_EMPTY_QUERY,
PGSQL_COMMAND_OK, PGSQL_TUPLES_OK, PGSQL_COPY_TO,
PGSQL_COPY_FROM, PGSQL_BAD_RESPONSE, PGSQL_NONFATAL_ERROR and
PGSQL_FATAL_ERROR.
See also pg_connection_status.
pg_send_query
Send asynchronous query
Descriptionboolpg_send_queryresourceconnectionstringqueryboolpg_send_querystringquerypg_send_query send asynchronous query to the
connection. Unlike
pg_query, it can send multiple query to
PostgreSQL and get the result one by one using
pg_get_result. Script execution is not block
while query is executing. Use
pg_connection_busy to check connection is
busy (i.e. query is executing) Query may be canceled by calling
pg_cancel_query.
Although, user can send multiple query at once. User cannot send
multiple query over busy connection. If query is sent while
connection is busy, it waits until last query is finished and
discards all result.
See also pg_query,
pg_cancel_query,
pg_get_result and
pg_connection_busypg_cancel_query
Cancel async query
Descriptionboolpg_cancel_queryresourceconnectionpg_cancel_query cancel asynchronous query sent by
pg_send_query. You cannot cancel query executed by
pg_query.
See also pg_send_query,
pg_cancel_result and
pg_connection_busypg_escape_bytea
Escape binary for bytea type
Descriptionstringpg_escape_byteastringdatapg_escape_string escapes string for
bytea datatype. It returns escaped string.
When you SELECT bytea type, PostgreSQL returns octal byte value
prefixed by \. (e.g. \032) Users are supposed to convert back to
binary formant by yourself.
This function requires PostgreSQL 7.2 or later. With PostgreSQL
7.2.0 and 7.2.1, bytea type must be casted when you enable
multi-byte support. i.e. INSERT INTO test_table (image)
VALUES ('$image_escaped'::bytea); PostgreSQL 7.2.2 or
later does not need cast. Exception is when client and backend
character encoding does not match, there may be multi-byte
stream error. User must cast to bytea to avoid this error.
Newer PostgreSQL will support unescape function. Support for
built-in unescape function will be added when it's available.
See also pg_escape_stringpg_escape_string
Escape string for text/char type
Descriptionstringpg_escape_stringstringdatapg_escape_string escapes string for
text/char datatype. It returns escaped string for
PostgreSQL. Use of this functon is recommended instead of
addslashes.
This function requires PostgreSQL 7.2 or later.
See also pg_escape_byteapg_metadata
Get metadata for table.
Descriptionarraypg_metadataresourceconnectionstringtable_namepg_metadata returns table definition for
table_name as array. If there is error, it
returns &false
This function is experimental.
See also pg_convertpg_convert
Convert associative array value into suitable for SQL statement.
Descriptionarraypg_convertresourceconnectionstringtable_namearrayassoc_arraypg_convert check and convert assoc_array suitable for SQL statement.
This function is experimental.
See also pg_metadatapg_insert
Insert array into table.
Descriptionboolpg_insertresourceconnectionstringtable_namearrayassoc_arrayboolconvertpg_insert inserts
assoc_array which has
field=>value into table specified as
table_name. If convert is
not specified or &true, pg_convert applied
to assoc_array.
pg_insert
]]>
This function is experimental.
See also pg_convertpg_select
Select records.
Descriptionarraypg_selectresourceconnectionstringtable_namearrayassoc_arrayboolconvertpg_select selects records specified by
assoc_array which has
field=>value. For successful query, it returns
array contains all records and fields that match the condition
specified by assoc_array. If
convert is not specified or &true,
pg_convert applied to
assoc_array.
pg_select
]]>
This function is experimental.
See also pg_convertpg_delete
Delete records.
Descriptionlongpg_deleteresourceconnectionstringtable_namearrayassoc_arrayboolconvertpg_delete deletes record condition specified by
assoc_array which has
field=>value. If convert is
not specified or &true, pg_convert applied
to assoc_array.
pg_delete
]]>
This function is experimental.
See also pg_convertpg_update
Update table.
Descriptionlongpg_updateresourceconnectionstringtable_namearrayconditionarraydataboolconvertpg_update updates records that matches
condition with data If
convert is not specified or &true,
pg_convert applied to
assoc_array.
pg_update
'AA', 'field2'=>'BB');
// This is safe, since $_POST is converted automatically
$res = pg_update($db, 'post_log', $_POST, $data);
if ($res) {
echo "Data is updated: $res\n";
}
else {
echo "User must have sent wrong inputs\n";
}
?>
]]>
This function is experimental.
See also pg_convert