Error prevention in mysql_connect () - Access denied

I have this piece of code:

$conn = mysql_connect($host, $usr, $pwd); 

How can I prevent PHP from printing an error message when access to MySQL is denied?

I need exactly this syntax, but nothing works for me.

I tried $conn = mysql_connect($host, $usr, $pwd) or false; and $conn = mysql_connect($host, $usr, $pwd) || false; $conn = mysql_connect($host, $usr, $pwd) || false; and the following:

 try { $conn = mysql_connect($host, $usr, $pwd); if(!$conn) { throw new Exception('Failed'); } } catch(Exception $e) { // ... } 

PHP always prints an error message that the connection failed. But I just want to return false if this fails.

+6
source share
4 answers

I don't know why you want, but can you add @ to hide php errors?

see http://php.net/manual/en/language.operators.errorcontrol.php

+6
source

Error printing should not be activated on the production server. Try changing the php.ini configuration to a log error, not a display.

Using the @to silent error message is not a good solution.

+10
source

You should not use mysql_connect in PHP 5, as it is now deprecated in favor of mysqli and PDO.

But you can possibly turn off the warning generated by mysql_connect with the @ prefix:

@mysql_connect()

+3
source

@ suppresses errors. And also set the error report error_reporting(0); to disable all errors. Also check mysqli

Documents

0
source

Source: https://habr.com/ru/post/897677/


All Articles