php - how to catch warnings for mysqli_connect() -


how catch warnings mysqli_connect()

example

if (!$connection2 = mysqli_connect($host, $username, $password, $name)){     die('mysql error: ' . mysql_error());  } 

or

$connection2 = mysqli_connect($host, $username, $password, $name); if (!$connection2){     die('mysql error: ' . mysql_error());  } 

so, trying test mysqli_connect() method putting wrong host, username etc in it. when put wrong variables in such put hostlocal instead of localhost. gives me error

warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed:
no such host known.

i know putting wrong host in want catch errors in appropriate way. decent message perfect like:

unfortunately, details entered connection incorrect!

so, how message instead of warning, thanks

for one, mysql_error() won't give anything. it's part of library, mysql_, doesn't interchange new , improved mysqli_. being said, can set mysqli throw exceptions, can catch.

the warnings still logged, if error-logging enabled (as should!). can catch error , display more user-friendly page if connection fails. should never display actual error messages live project - in development fine, never on live environment.

mysqli_report(mysqli_report_error | mysqli_report_strict); // set mysqli throw exceptions   try {     $connection2 = mysqli_connect($host, $username, $password, $name); } catch (mysqli_sql_exception $e) {     // $e exception, can use wish      die("unfortunately, details entered connection incorrect!"); } 

this log same warning you're getting log,

php_network_getaddresses: getaddrinfo failed: name or service not known

although won't displayed page.

you can suppress warning (so won't logged) adding @, don't recommend unless explicitly implement own error-handling/reporting. errors should logged.

mysqli_report(mysqli_report_error | mysqli_report_strict); // set mysqli throw exceptions   try {     $connection2 = @mysqli_connect($host, $username, $password, $name); } catch (mysqli_sql_exception $e) {     error_log($e->getmessage()); // log error manually     die("unfortunately, details entered connection incorrect!"); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -