Search code examples
phpmysqlsqlmysqliopencart

MySQLI Connect Error Shows Password


I have a website running on opencart which uses command Mysqli connect to connect to database, now the problem i am facing is if due to some error it is not able to connect or lets say i change the password for my database user my Output shows a fatal error which includes the password used to connect also.

Error i Receive

Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'saledart_admin'@'localhost' (using password: YES) in /home2/saledart/public_html/system/database/mysqli.php on line 6

Fatal error: Uncaught exception 'ErrorException' with message 'Error: Could not make a database link (1045) Access denied for user 'saledart_admin'@'localhost' (using password: YES)' in /home2/saledart/public_html/system/database/mysqli.php:9 Stack trace:> #0 /home2/saledart/public_html/vqmod/vqcache/vq2-system_library_db.php(13): DBMySQLi->__construct('localhost', 'saledart_admin', 'SqlPassword', 'saledart_db') #1 /home2/saledart/public_html/index.php(44): DB->__construct('mysqli', 'localhost', 'saledart_admin', 'SqlPassword', 'saledart_db') #2 {main} thrown in /home2/saledart/public_html/system/database/mysqli.php on line 9

As you can see such error displays my sql password, i want to know if there is a way that my password is not shwn ever due to any error like this.

I hope i was able to make myself clear.

Regards

PS: I Can resolve this error since this is coming since i have changed the Database users password but my point is that even old password should not be shown such easily.


Solution

  • use php try catch , its Exception handling is used to change the normal flow of the code execution if a specified error occurs.

    try {
        $con = mysqli_connect("localhost","my_user","my_password","my_db");
    
        if(!$conn) {
            throw new Exception('Failed');
        }
    } catch(Exception $e) {
        echo 'Server error. Please try again some time.';
        die;
    
    }