New MySQLi(): How to Intercept an 'Unable to Connect' Error

new mysqli(): how to intercept an 'unable to connect' error?

You need to tell mysqli to throw exceptions:

mysqli_report(MYSQLI_REPORT_STRICT);

try {
$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db') ;
} catch (Exception $e ) {
echo "Service unavailable";
echo "message: " . $e->message; // not in live code obviously...
exit;
}

Now you will catch the exception and you can take it from there.

MySQLI Connect Error Shows Password

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;

}

Can a MYSQLI connection be reopened WITHOUT instantiating a new object

No, you have to instantiate a new MYSQLI object. You can use the same variable $mysqli though but you have to write this code again:

 $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

PHP/MySQLi - Says I am connected to database even when there is invalid user

It's hard to be certain what is going on without seeing your code that creates $con. However, my guess is that your problem comes from a quirk of PHP: __construct functions will always return an object. This will be a truthy value, so will pass if tests.

In this case, you think new mysqli(...) will return false if the settings are wrong. It won't. It will return a mysqli object that is not connected to the database.

You need to test the mysqli::$connect_error property instead:

if (!$con->connect_error) { echo 'Successfully connected to your database'; }
if ($con->connect_error) { echo 'We were unsuccessful in connecting to your database, please <a href="#">Go back and reconfigure configuration file</a>.'; }

Connect mysqli without print error

Well if you don't want errors to be printed - just tell PHP not to print them.

Configure PHP this way:

display_errors = 0
log_errors = 1

if you want to catch errors in some handler, you have to register it first.

Cloud SQL using mysqli error

You've set your database name to null. A connection is made like so:

$mysqli = new mysqli("localhost", "user", "password", "database");

The mysqli constructor can take in the following parameters (in this order):

$mysqli = mysqli($hostname, $username, $password, $database, $port, $socket);

In your case, you've set the parameters to be:

$hostname = null; //Defaults to mysqli.default_host
$username = "root";
$password = "";
$database = null; //Defaults to ""
$port = null; //Defaults to mysqli.default_port
$socket = "/cloudsql/db-php-001:db-instance-001";

mysqli::query(): Couldn't fetch mysqli

Probably somewhere you have DBconnection->close(); and then some queries try to execute .


Hint: It's sometimes mistake to insert ...->close(); in __destruct() (because __destruct is event, after which there will be a need for execution of queries)



Related Topics



Leave a reply



Submit