mysql_[p]connect() and mysql_error()/mysql_errno()

From: Date: Mon, 23 Jul 2001 16:25:11 +0000
Subject: mysql_[p]connect() and mysql_error()/mysql_errno()
Groups: php.db 
Request: Send a blank email to php-db+get-10684@lists.php.net to get a copy of this message
I see in the PHP 4.0.6 release notes that mysql_error() and mysql_errno() have been fixed so that you can get error information from them when a connect call fails. (Previously they worked only after successfully establishing a connection, so that you had to use $php_errormsg with track_errors turned on to get connection error information.) I want to write connection code that takes advantage of this, so that it uses the MySQL error functions if they're the fixed versions, and falls back to $php_errormsg otherwise. Does the following look reasonable? if (!($conn_id = @mysql_connect ($host, $user, $password))) {
    # If mysql_errno()/mysql_error() work for failed connections, use
    # them (invoke with no arguments). Otherwise, use $php_errormsg.
    if (mysql_errno ())
    {
        die (sprintf ("Cannot connect to database server: %s (%d)\n",
                htmlspecialchars (mysql_error ()),
                mysql_errno ()));
    }
    else
    {
        die ("Cannot connect to database server: "
                . htmlspecialchars ($php_errormsg) . "\n");
    }
} I'm assuming that the proper way to invoke mysql_error()/mysql_errno() after a failed connection is with *no* argument, because when a failure occurs $conn_id won't have any reasonable value and shouldn't be passed to those functions. -- Paul DuBois, paul@snake.net

« previous php.db (#10684) next »