Re: No mysql_error() from mysql_connect()

From: Date: Fri, 29 Dec 2000 01:07:23 +0000
Subject: Re: No mysql_error() from mysql_connect()
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-5499@lists.php.net to get a copy of this message
At 11:46 PM +0100 12/28/00, Christoph Ender wrote:
Hi, When trying to connect to a MySQL Database, I try catch errors with mysql_error(). That works fine, but not for mysql_connect().
That's right. mysql_error() and mysql_errno() cannot report errors properly until a valid connection has been established. That means they're useless for reporting errors that occur during the connection attempt. Try printing the value of $php_errormsg instead: # Demonstrate error-checking with mysql_error() and mysql_errno(). # This also handles mysql_connect(), for which mysql_error() and # mysql_errno() don't work. Use of $php_errormsg assumes that # you have track_errors turned on in your PHP configuration file. #@ FRAG if (!($conn_id = @mysql_connect ("host", "user", "password"))) {
    die ("Cannot connect to database server: "
            . htmlspecialchars ($php_errormsg) . "\n");
}
Example: ------------------------------------------------------------------------ <?php $linkid = mysql_connect("localhost", "myusername", "mypassword"); echo mysql_errno().": ".mysql_error()."<BR>"; ?> ------------------------------------------------------------------------ returns the following: ------------------------------------------------------------------------ Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111) in /usr/local/httpd/htdocs/ModernMarket/t1.php3 on line 3 : ------------------------------------------------------------------------ So just a single colon, no messages. Turning off the warnings like this... ------------------------------------------------------------------------ <?php $old_error_reporting = error_reporting(E_ALL & ~(E_WARNING )); $linkid = mysql_connect("localhost", "myusername", "mypassword"); echo mysql_errno().": ".mysql_error()."<BR>"; error_reporting($old_error_reporting); ?> ------------------------------------------------------------------------ ... leaves me just with a colon. I can test for a valid database connection with "if ($link) ..." but I'd like to catch the mysql error that caused the connection-failure. Any ideas? Someone succeeded here? Thanks in advance, Christoph. -- Christoph Ender chrender@moondock.org http://www.moondock.org/chrender/ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net For additional commands, e-mail: php-db-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
-- Paul DuBois, paul@snake.net

« previous php.db (#5499) next »