3

I'm wondering which one should I use? mysqli_connect_error or mysqli_connect_errno

public function connect() {

    $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if (!$this->connection->connect_error) {

        die("Connection Failed" . " " . $this->connection->connect_error);

    }

}

Or

public function connect() {

    $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if (!$this->connection->connect_errno) {

        die("Connection Failed" . " " . $this->connection->connect_error);

    }

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Alex
  • 1,451
  • 5
  • 15
  • 16
  • 2
    You should remove '!' from both of if clause as connect_error and connect_errno return true value if an error exists. – Nasser Tahani Mar 08 '18 at 15:58

1 Answers1

3

For your question it's just irrelevant, you can use either.

But let me suggest you a better way.
Instead of checking for the errors manually, just tell mysqli to report errors automatically:

public function connect()
{
   mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
   $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
}

It will give you a more informative error message and it will work for all mysqli commands, so you will need no error checking for prepare/execute and other mysqli commands as well.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345