2

I'm having some troubles with PostreSQL arrays and html forms. Let's say I have a text field in a webpage where users can write a comma-separated list of strings, like for example a list of names. This list is stored in a Postgres db.

When the page is requested,

1) My php code fills the textfield with SELECT name_list FROM names; and what I get is something like:

|  name_list         |
|____________________|
| {alice,bob,carl}   |
| ....               |
|                    |

2) Remove the first and the last char (the brackets) in order to get a printable string:

..
$arrayWithoutBrackets = substr($row['name_list'], 1,-1);
echo($arrayWithoutBrackets) ---->  alice,bob,carl
..

That string is what the users can read and, eventually, modify and send back to the server using a simple html form.

But what if the user inserts a name containing blank spaces (like alice,bob,carl,john doe)? That's what happens after updating the table:

|  name_list                    |
|_______________________________|
| {alice,bob,carl,"john doe"}   |
| ....                          |
|                               |

Postgres is auto-quoting my string and when the users will reload the page they will see

alice,bob,carl,"john doe"

NOT

alice,bob,carl,john doe

Manually searching for quote characters and removing them using str_replace() is not an option since I have many of those array-based text fields in my application (hundreds of them).

Is there any way to solve this?

4
  • What is an 'array-based textfield' supposed to be? Is the column an array or text? If it is an array how are you parsing the array contents? If it is text why do you store an array in it? Commented Nov 24, 2014 at 12:40
  • Always include your version of Postgres - and ideally the definition of the table. Commented Nov 24, 2014 at 12:40
  • @ Richard Huxton A 'array-based textfield' is my way to define a textfield filled with the value of an array of strings. name_list is an array of varchars and i parse it is removing the brackets. Commented Nov 24, 2014 at 13:04
  • @ Erwin Brandstetter I'm running PostgreSQL 9.3 and the definition is too long and irrelevant i think. Btw, i have many other columns in the same table. Commented Nov 24, 2014 at 13:09

1 Answer 1

2

The double-quotes you see are just decorators for unambiguous input / output added by Postgres. Your method of removing leading and trailing curly braces is pretty hackish.

To get proper output, use array_to_string():

SELECT array_to_string(namelist, ', ') AS namelist_raw
FROM   names

SQL Fiddle.

Be aware, however, that Postgres uses the quotes for a reason. If the separator-string (', ' in the example) is included in any element of the array, the output text is ambiguous.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. It's very useful. What if i have multiple columns and i need to select *?
@user3067250: You can just combine that with any number of columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.