Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to use PHP PDO to insert values into a PostgreSQL database, but encounter the following error message:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: array value must start with "{" or dimension information

The field type is a PostgreSQL Array .

Here is my code (simplified):

try {
  $sql='INSERT INTO table (fieldName, foo) VALUES (?, ?)';
  $fieldName=array('1','2');
  $data=array($fieldName, 'bar'); # fieldName is array type in PostgreSQL
  $STH = $conn->prepare($sql);          
  $STH->execute($data);
catch(PDOException $e) {
  # Handle exception
}

Thanks!

share|improve this question
1  
Why $data=array(array('1','2')); instead of $data=array('1','2');? –  George Cummins Jun 21 '13 at 21:38
    
I updated the question. The fieldname variable is stored as an array in PostgreSQL. –  Matt Jun 21 '13 at 21:41

1 Answer 1

up vote 3 down vote accepted

Just in case anyone ever comes across this, the solution was to implode the array and add {}.

$fieldName='{'.implode(",",$fieldName).'}';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.