I have view similar to this one
CREATE OR REPLACE VIEW regexp_test AS (
SELECT regexp_matches(decode(\'NTB4\', \'base64\')::text, \'(\\d+)x\')
)
<
I figured out using encode(decode('NTB4', 'base64'), 'escape')
instead of typecast decode('NTB4', 'base64')::text
fixed problem.
So test view now looks like this:
CREATE OR REPLACE VIEW regexp_test AS (
SELECT regexp_matches(encode(decode('NTB4', 'base64'), 'escape'), '(\d+)x')
)
Calling pg_query('SELECT * FROM regexp_test')
now returns expected result - single row/field with '{50}'
in it.
pg_query
returns a result resource.
$result = pg_query('SELECT * FROM regexp_test');
while ($row = pg_fetch_row($result)) {
echo "$row";
}
pg_query returns false on error
If an error occurs, and FALSE is returned, details of the error can be retrieved using the pg_last_error() function if the connection is valid.
The same query
select e'\\x353078'::bytea;
gives results in different formats in psql:
bytea
----------
\x353078
and in PgAdmin III:
bytea
----------
50x
For the documentation:
The bytea type supports two external formats for input and output: PostgreSQL's historical "escape" format, and "hex" format. Both of these are always accepted on input. The output format depends on the configuration parameter
bytea_output
; the default is hex. (Note that the hex format was introduced in PostgreSQL 9.0; earlier versions and some tools don't understand it.)
PgAdmin III (and also PgAdmin4) probably for historical reasons sets the value of bytea_output
to escape
while the default value of the parameter is hex
. This can lead to confusion (and as you can see it leads). It seems that pgAdmin should not change the default value of the parameter.
You can change the parameter in your application to get the same behaviour like in PgAdmin:
set bytea_output to escape;
Of course, using encode()
is also a good solution.