1

I cannot seem to figure this out. I wrote a PHP script and I keep getting a Array to string conversion in... Notice as an error when trying to execute the postgres query:

$tables = array(array('a','table1'),array('c','table2'));
$table = $tables[0][1];
$query = "SELECT * FROM " . $table . " WHERE id = :id LIMIT 1";
$select = $dbconn->prepare($query);
$id = $_GET['id'];
if($select->execute()){
}else{
    echo $select->errorInfo();
}

I am using PostgreSQL version 9.2.1 and the latest PHP

1 Answer 1

2

PDOStatement::errorInfo() returns an array, but you are directly echoing it, which will always result in the string Array. You must store its returned array, and access the components you wish to read:

$err = $select->errorInfo();
print_r($err);

// Example as lifted from PHP docs linked above:
PDO::errorInfo():
Array
(
    [0] => HY000
    [1] => 1
    [2] => near "bogus": syntax error
)

Incidentally, the E_NOTICE for array to string conversion is new in PHP 5.4. The same code in PHP 5.3 would output Array as a string but would not issue the E_NOTICE.

Since you are using PHP 5.4, you can dereference the message [2] directly if you wish:

// 5.4+ only...
echo $select->errorInfo()[2];

Now why you are getting an error:

As to why you are entering the else block to report the error in the first place, you have a place holder for :id, but you never bound the variable $id before executing the query.

You may use

$select->execute(array(':id', $id))

Or

$select->bindParam(':id', $id, PDO::PARAM_INT); // assuming an integer $id. Use PDO::PARAM_STR for strings
Sign up to request clarification or add additional context in comments.

Comments

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.