Re: [GENERAL] CentOS 7.3, PostgreSQL 9.6.2, PHP 5.4 deliver arrayas string

Поиск
Список
Период
Сортировка
От rob stone
Тема Re: [GENERAL] CentOS 7.3, PostgreSQL 9.6.2, PHP 5.4 deliver arrayas string
Дата
Msg-id 1488508831.6297.1.camel@gmail.com
обсуждение исходный текст
Ответ на [GENERAL] CentOS 7.3, PostgreSQL 9.6.2, PHP 5.4 deliver array as string  (Alexander Farber <alexander.farber@gmail.com>)
Ответы Re: [GENERAL] CentOS 7.3, PostgreSQL 9.6.2, PHP 5.4 deliver array as string  (Alexander Farber <alexander.farber@gmail.com>)
Список pgsql-general
Hi Alex,

On Thu, 2017-03-02 at 21:45 +0100, Alexander Farber wrote:
> Good evening!
>
> I am calling this stored function - 
>
> CREATE OR REPLACE FUNCTION words_get_board(
>                 in_gid integer
>         ) RETURNS TABLE (
>                 out_bid integer,
>                 out_letters varchar[15][15],
>                 out_values integer[15][15]
>         ) AS
> $func$
>         SELECT 
>                 bid,
>                 letters,
>                 values
>         FROM    words_games
>         WHERE   gid = in_gid;
>
> $func$ LANGUAGE sql STABLE;
>
> by a PHP script -
>
>         $sth = $dbh->prepare(SQL_GET_BOARD);
>         $sth->execute(array($gid));
>         if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
>                 $bid     = $row['bid'];
>                 $letters = $row['letters'];
>                 $values  = $row['values'];
>         }
>
>

Don't use PDO but assuming you want to loop through a result set,
shouldn't this line:-

if ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

be

> while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

Also you need to test if the result set is empty or not.

I don't have an "easy" way to convert Postgres arrays into PHP style
arrays. What I ended up doing was to use the array_to_string function
in the select statement using the underscore character as the delimiter
in the string, and then converting the string to a PHP array via an
explode. However, I've only had to do this with single level arrays.

Don't know if this helps as you haven't supplied the table definition
of words_games.

Cheers,
Rob



В списке pgsql-general по дате отправления:

Предыдущее
От: Michael Paquier
Дата:
Сообщение: Re: [GENERAL] Postgres 9.x table/index stats reset timestamp
Следующее
От: George Neuner
Дата:
Сообщение: [GENERAL] DISTINCT vs GROUP BY - was Re: is (not) distinct from