473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[PDO] Why does it print "Array"?

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
4 3136
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
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
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vic Spainhower | last post by:
Hello, I am new to php and MySQL and I'm attempting to run a select query on a MySQL database which is working fine except prior to displaying the table with the results from the select query it...
6
by: sonic | last post by:
what exactly is the difference ? I think that is more suitable for declarative arrays, but not exactly sure why. TIA
4
by: Greg Stark | last post by:
I find myself wishing I had a syntax "LIKE ANY (array)". I don't see much value in the = ANY, = ALL, <> ANY, <> ALL syntax since they're equivalent more or less to IN and NOT IN. But it could be...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
0
by: John Haigh | last post by:
Should possibly have an "add" method in a PostingObject class in the constructor of PostingObject. will call to add a Posting to the ? Does this make sense? My sense is that a PostingObject needs a...
14
by: Alex | last post by:
I saw the topic of "wired code " about "point to array" and know a little about it. But I am still confused about the question below: I define a point to array "b" int (*b); then I locate the...
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.