views:

16

answers:

2

I am connecting to MySQL database on my webpage and have this copy-pasted code for errors:

if(DB::isError($db)) die($db->getMessage());

I have the connection code in an outside file called connection.inc that I include at the beginning of my page before the DOCTYPE and html tags.

For debugging purposes, how can I print the database errors on my webpage?

I thought I could do something like this:

echo 'Could not connect to database. The error was:' . $db->getMessage();

but this returns: Fatal error: Call to undefined method DB_mysql::getMessage()

The connection.inc looks like this:

require_once("DB.php");
$dsn="mysql://mannerv_bonus:blahblah@localhost/mannerv_bonus";
$db = DB::connect($dsn);
if(DB::isError($db)) die($db->getMessage());
else $db->query("SET NAMES 'latin1'");

I tried this:

if(DB::isError($db)) {
echo 'There was an error';
echo $db->getMessage();
}

I can change the password and do whatever but it seems that the isError is never true. I thought that if the password is wrong, this would be an error.

A: 

It fails because it isn't a PEAR Error object. (Which means the db connect was successful)

if (PEAR::isError($db)) {
    echo $db->getMessage();
}

But the die at the top (in your connection.inc) will stop the script execution immediately and any code below it will never be run.

nikc
This is what puzzles me; I still get the same error even if I change my password to some blah blah or otherwise mess with my database connection, ie. it seems that I am never able to print out any errors even though there for sure are some.
Markus Ossi
Does the script `die` when you change the password? How does your `connection.inc` look like?
nikc
The connection.inc looks like this: require_once("DB.php"); $dsn="mysql://mannerv_bonus:blahblah@localhost/mannerv_bonus"; $db = DB::connect($dsn); if(DB::isError($db)) die($db->getMessage()); else $db->query("SET NAMES 'latin1'");I tried this: if(DB::isError($db)) { echo 'There was an error'; echo $db->getMessage(); }I can change the password and do whatever but it seems that the isError is never true. I thought that if the password is wrong, this would be an error.
Markus Ossi
Ok that did not come out right.
Markus Ossi
What if you change the database or host name?
nikc
No reaction. Seems that I can put whatever and there are never any errors.
Markus Ossi
That's odd. I've no idea how to help you further. Sorry.
nikc
A: 

The connection.inc looks like this:

require_once("DB.php");
$dsn="mysql://mannerv_bonus:blahblah@localhost/mannerv_bonus";
$db = DB::connect($dsn);
if(DB::isError($db)) die($db->getMessage());
else $db->query("SET NAMES 'latin1'");

I tried this:

if(DB::isError($db)) {
echo 'There was an error';
echo $db->getMessage();
}

I can change the password and do whatever but it seems that the isError is never true. I thought that if the password is wrong, this would be an error.

Markus Ossi