Noob question re: why I can't get error message or number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bartolo
Forum Newbie
Posts: 3
Joined: Thu Jan 01, 2009 3:17 pm

Noob question re: why I can't get error message or number

Post by bartolo »

HNY!!

I cannot get $mysqli->error or $mysqli->errno to react to a forced error.

<?php
// Connect to the db
$mysqli = new mysqli("localhost", "user", "BADpassWord", "db") // this does create the error

// Check connection
if ($mysqli->errno) // no response from this
{
printf( "The error message is: %s", $mysqli->error); // no response from this
}
?>

PHP does catch the error, but I cannot get MySQL to let me intercede.

I have also tried:

<?php
$mysqli = new mysqli("localhost", "user", "BADpassWord", "db")
or die("Connection failed");
echo "I made it this far";
?>

and "I made it this far" gets printed out to the browser, along with the PHP error message. But it totally ignores the die().

Any help would be appreciated.

Thanks
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Noob question re: why I can't get error message or number

Post by it2051229 »

try:

Code: Select all

 
// Returns a MySQL link identifier on success, or FALSE on failure. 
mysql_connect("localhost", "username", "bad password") or die(mysql_error());
 
but most of the time we dont do:

Code: Select all

 
$someVariable = mysql_connect("localhost", "username", "bad password") or die(mysql_error());
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Noob question re: why I can't get error message or number

Post by requinix »

Sorry person with a seemingly random username, I have to disagree with that. Apart from OP trying to use mysqli and not mysql, the "most of the time we don't do" isn't accurate.

bartolo:
1. If there's an error connecting you need to use ::connect_errno and ::connect_error instead.
2. new will always return an object, and an object is never treated as false. The "or die" will never happen.
Post Reply