Postgresql regexp_matches inside view always returns null when queried from PHP

后端 未结 3 1063
眼角桃花
眼角桃花 2020-12-22 12:21

I have view similar to this one

CREATE OR REPLACE VIEW regexp_test AS (
    SELECT regexp_matches(decode(\'NTB4\', \'base64\')::text, \'(\\d+)x\')
)
<         


        
相关标签:
3条回答
  • 2020-12-22 12:40

    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.

    0 讨论(0)
  • 2020-12-22 12:43

    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.

    0 讨论(0)
  • 2020-12-22 12:55

    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.

    0 讨论(0)
提交回复
热议问题