Customize error messages on database connection

Asked

Viewed 824 times

4

Good here in the stack I found some topics that helped me, I’m new with PHP, and I wanted to make my connection return a custom message without making it show those warnings.

Only when I add the check nothing happens, my complete code below.

<?php
class Connect {

    private $Connect;

    public function Connect() {
        $this->Connection();

        if(connection_aborted() == TRUE) {
            $this->Connection();
        }
    }

    public function Connection() {
        global $Config;

        $this->Connect = new mysqli($Config['mysql']['hostname'], $Config['mysql']['username'], $Config['mysql']['password'], $Config['mysql']['database'], $Config['mysql']['dataport']);

        if($this->Connect == false){
            exit('Falha na conexão, verifique Config.php');
        }

        return false;
    }
}
?>

2 answers

5


It can customize the error message by turning the errors returned by execptions with mysqli_report to avoid warnings related to indexes prefer: MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT in place of: MYSQLI_REPORT_ALL.

Add a Try in the block where you want to launch Exception and use the catch to display the custom message, remember to implement a log routine with the bank error to facilitate error detection.

public function __construct() {
    //linha que torna os erros em exceptions
    mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
    $this->Connection();

    if(connection_aborted() == TRUE) {
        $this->Connection();
    }
}

public function Connection() {

    global $Config;
    try {
        $this->Connect = new mysqli($Config['mysql']['hostname'],
                $Config['mysql']['username'], $Config['mysql']['password'],
                $Config['mysql']['database'], $Config['mysql']['dataport']);

    } catch (Exception $e) {
        exit('Falha na conexão, verifique Config.php');
    }

Prefer to define the constructor as a constructor and not depend on luck, from php 5.3.3 the mechanism changed. A method with the same name as php class within a namespace is not treated as a builder.

Constructors - manual

Prefer:

function __construct() {

In place of:

public function Connect() {
  • Thank you you answered my question correctly... it worked perfectly.

5

I recommend using PDO, since it is starting this will make your life much easier. It supports more than 10 types of database. An example connection with verification support is below:

<?php
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
        foreach($dbh->query('SELECT * from FOO') as $row) {
            print_r($row);
        }
       $dbh = null;
    } catch (PDOException $e) {
       print "Error!: " . $e->getMessage() . "<br/>";
       die();
    }
?>
  • 1

    Sometimes it is necessary to make clear to the PDO to launch exceptions with the setAttribute or directly in the builder.

  • 1

    I am studying mysqli first, I tried PDO but found it much harder, yes the time I finish my mini project I will study object-oriented PDO!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.