0

That is i write in oops it show mysql error message, that i don't want, i wan't only custom MSG.

class Myfunction{

    private $DBHOST = 'localhost';
    private $DBUSER = 'rot';
    private $DBPASS = '';
    private $DBNAME = 'database';
    public $conn;

    public function __construct(){
        try{
            $this->conn = mysqli_connect($this->DBHOST, $this->DBUSER, $this->DBPASS, $this->DBNAME);
            if(!$this->conn){  
                throw new Exception('Connection was Not established');
            }
        }
        catch(Exception $e) {
            echo 'Message: ' .$e->getMessage();

        }

    }
}

OutPut :-

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'rot'@'localhost' (using password: NO) in C:\xampp\htdocs\CRUD-PHP\config\CUFunction.php on line 13

Message: Connection was Not Extablish

Now Question is How to remove " warning " MSG??, I want to only my custom MSG

One Possible Solution found however, i think it is not appropriate

For Example :

$this->conn = @mysqli_connect($this->DBHOST, $this->DBUSER, $this->DBPASS, $this->DBNAME);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Zobjoys Jeirmov
  • 181
  • 1
  • 5
  • 16
  • Change this echo 'Message: ' .$e->getMessage(); to your custom message in catch block. – Prabhjot Singh Kainth Nov 27 '19 at 04:57
  • 1
    Try to turn off PHP's error reporting, however, do this for your production only, it's not recommended during development environment since PHP error report has more information about the error. See here: https://stackoverflow.com/questions/10711517/turning-error-reporting-off-php – catcon Nov 27 '19 at 04:58
  • Enable exception mode instead. – Dharman Nov 27 '19 at 07:50
  • See [how to enable custom error messages for all PHP errors](https://phpdelusions.net/articles/error_reporting#error_page) – Your Common Sense Nov 27 '19 at 07:55

1 Answers1

-3

As you said yourself, the @ prefix is an error control operator. If you will be handling your own errors, you could theoretically suppress that. It is usually recommended not to suppress errors though, they are there for a reason :).

PHP.net even states

Warning Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

Be cautious while using this!

class Myfunction{

private $DBHOST = 'localhost';
private $DBUSER = 'rot';
private $DBPASS = '';
private $DBNAME = 'database';
public $conn;

public function __construct(){
    try{
        $this->conn = @mysqli_connect($this->DBHOST, $this->DBUSER, $this->DBPASS, $this->DBNAME);
        if(!$this->conn){  
            throw new Exception('Connection was Not established');
        }
    }
    catch(Exception $e) {
        echo 'Message: ' .$e->getMessage();

    }

    }
}