PHP 7.1.21 Released


(PHP 4 >= 4.3.0, PHP 5, PHP 7)

pg_convert Converte os valores de um array associativo em uma declaração SQL apropriada.


array pg_convert ( resource $connection , string $table_name , array $assoc_array [, int $options ] )

pg_convert() checa e converte assoc_array em uma declaração SQL apropriada. Pré-condição para pg_convert() é a existencia de uma tabela table_name a qual teve ter tantos campos quantos os elementos em assoc_array. Os nomes dos campos em e os valores em table_name devem ser iguais aos índices e valores em assoc_array. Retorna uma matriz com os valores convertidos em caso de sucesso ou se não, FALSE.


Esta função é EXPERIMENTAL. O comportamento, seu nome e documentação podem mudar sem aviso em futuras versões do PHP. Utilize por sua própria conta e risco.

Veja também pg_meta_data().

add a note add a note

User Contributed Notes 6 notes

14 years ago
The only options that I see are:

PGSQL_CONV_IGNORE_DEFAULT  - Do not use DEAFULT value by removing field from returned array
PGSQL_CONV_FORCE_NULL - Convert to NULL if string is null string

These are constants, so don't quote them or anything.
4 months ago
Another thing that's not well documented is that (as of PHP 7.0/7.1) pg_convert doesn't like non-scalar types and will fail (but not throw just emit an E_WARNING and return false) if you pass it anything other than a string or a number, including an array or something like a DateTime. If you want to insert those types, you actually have to convert those yourself.

Also, somewhat surprisingly, $table_name is not compatible with the output of pg_​escape_​identifier, or seemingly any other kind of escaping.
gorhas at gmail dot com
3 years ago
There is a problem when using interval.
If in the array
"time_pause" => '00:30:00'
and time_pause is an interval
the insert fails
pg_insert(): '00:30:00' does not match with  '^(@?[ \t]+)?((([-+]?[ \t]+)?[0-9]+(\.[0-9]*)?[ ...
dharana at dharana dot net
15 years ago
I've found "options" possible values:

PG_CONV_CHECK - check only
PG_CONV_STRICT - raise warning for non fatal error
PG_CONV_QUOTE - add quote around values for vchar, text datetime.
PG_CONV_SLASH - add slashes if it needed.
PG_CONV_NULLCHK - check values are defined for NOT NULL fields.
PG_CONV_NO_DEFAULT - ignore default value even if value is empty string.
Hayley Watson
1 year ago
This will only apply the appropriate escaping and such appropriate for embedding the PHP value into an SQL statement.

It does (by default) check for nulls when the column is marked NOT NULL, and it will complain about trying to convert strings for an integer column (floats will be truncated).

Beyond the barest checking of syntax, however, it does NOT verify that the given value is a legitimate value for the column type.

// Assuming smallints.smallintis a smallint (-32768..32767) type column
32768// bogus value for smallint type
45.8,   // gets truncated to 45
400000, // bogus value for smallint type
] as $smallint)
$tmp = ['smallint' => $smallint];
$vals = pg_convert($dbconn, 'smallints', ['smallint' => $smallint]);
$vals['"smallint"'],"\n"// Notice the column name is also made SQL-safe

// Assuming uuids.uuid is a UUID type column
'Invalid Not-a-UUID',
    ] as
$tmp = ['uuid' => $uuid];
$vals = pg_convert($dbconn, 'uuids', ['uuid' => $uuid]);


All of the above data values will be "converted" - even the invalid ones - without complaint.
Andrew Falanga
13 years ago
Why does this function, pg_convert(), reject the string 'TRUE' as a valid value for the boolean types?  I'm using PHP 4.3.9 and my web page originally set the value for submission to update, to 'TRUE'.  pg_convert() threw it back to me saying that it's not a valid value for boolean types.  However, the postgresql website says otherwise.  See for the context of my question/comment.
To Top