By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

[PDO] Why does it print "Array"?

P: n/a
Hello

I'm puzzled as to why PHP (5) prints this (under FreeBSD 6.3):

=========
#!/usr/local/bin/php
<?php

$dbh = new PDO("sqlite:test.sqlite");
$sql = "CREATE TABLE IF NOT EXISTS calls (calls_id INTEGER PRIMARY
KEY, calls_sent CHAR)";
print "here\r\n";
$dbh->exec($sql) or die($dbh->errorInfo());
print "there\r\n";

?>
=========
here
Array
=========

Thank you.
Sep 5 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Gilles Ganault wrote:
$dbh->exec($sql) or die($dbh->errorInfo());
Array
$dbh->errorInfo() returns an array, which is printed by die().
Sep 5 '08 #2

P: n/a
On Sep 5, 11:39*am, Gilles Ganault <nos...@nospam.comwrote:
Hello

I'm puzzled as to why PHP (5) prints this (under FreeBSD 6.3):

=========
#!/usr/local/bin/php
<?php

$dbh = new PDO("sqlite:test.sqlite");
$sql = "CREATE TABLE IF NOT EXISTS calls (calls_id INTEGER PRIMARY
KEY, calls_sent CHAR)";
print "here\r\n";
$dbh->exec($sql) or die($dbh->errorInfo());
print "there\r\n";

?>
=========
here
Array
=========

Thank you.
Alternatively, you can use or

die(print_r($dbh->errorInfo(), true));

but i think you should probably be looking for another construct for
your error checking.
Sep 5 '08 #3

P: n/a
On Fri, 05 Sep 2008 20:59:13 +0200, Sjoerd <sj******@gmail.comwrote:
>$dbh->errorInfo() returns an array, which is printed by die().
Thanks. The funny thing is that the $sql statement is correct, so I
don't understand why die() is being run at all :-/

This works fine:
$dbh = new PDO("sqlite:test.sqlite");
$sql = "CREATE TABLE IF NOT EXISTS calls (calls_id INTEGER
PRIMARY KEY, calls_sent CHAR)";
print "here\r\n";
//$dbh->exec($sql) or die($dbh->errorInfo());
$dbh->exec($sql);
print "there\r\n";

Also, what should I do to actually display the error message sent by
die(errorInfo)?

Thank you.
Sep 5 '08 #4

P: n/a
..oO(Gilles Ganault)
>On Fri, 05 Sep 2008 20:59:13 +0200, Sjoerd <sj******@gmail.comwrote:
>>$dbh->errorInfo() returns an array, which is printed by die().

Thanks. The funny thing is that the $sql statement is correct, so I
don't understand why die() is being run at all :-/
http://www.php.net/manual/en/pdo.exec.php

| PDO::exec() executes an SQL statement in a single function call,
| returning the number of rows affected by the statement.

For a CREATE statement it returns 0, hence your script will die.
>This works fine:
$dbh = new PDO("sqlite:test.sqlite");
$sql = "CREATE TABLE IF NOT EXISTS calls (calls_id INTEGER
PRIMARY KEY, calls_sent CHAR)";
print "here\r\n";
//$dbh->exec($sql) or die($dbh->errorInfo());
$dbh->exec($sql);
print "there\r\n";

Also, what should I do to actually display the error message sent by
die(errorInfo)?
You could use print_r() or var_export() to turn the array returned by
PDO::errorInfo() into a string. See the manual for details and required
parameters.

http://www.php.net/manual/en/function.print-r.php
http://www.php.net/manual/en/function.var-export.php

But much better would be an explicit error handling without using the
die() shortcut.

Micha
Sep 5 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.