Jump to content

MySQLi and exceptions


9three

Recommended Posts

Hey,

 

I'm having an issue. It's always returning "connected" whether the data base name is correct or not.

 

  public function connect()
  {
    $this->link = new mysqli($this->host, $this->username, $this->password, $this->dbname);
    if ($this->link->connect_error) { // Error returned true
      Throw New Exception($this->link->connect_error);
    }
    return $this->link; //No exception was thrown, return $this->link
  }

 

This is the index:

 

try {
  $mysqli = new mysql('localhost', 'root', '', 'cms2');
  echo 'Connected';
}
catch (Exception $e) {
  echo $e;
}

 

the database name is actually 'cms' not 'cms2'. But either way it prints "connected"

 

If I do !$this->link->connect_error it returns an exception

 

exception 'Exception' in C:\Users\9three\Desktop\Server\htdocs\library\mysqli.package.php:23 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\library\mysqli.package.php(16): mysql->connect() #1 C:\Users\9three\Desktop\Server\htdocs\library\index.php(12): mysql->__construct('localhost', 'root', '', 'cms2') #2 {main} 

 

Again, whether I use 'cms' or 'cms2' it still throws this exception. Now I don't know why, the syntax looks correct in the class.

 

I'm still learning to use MySQLi.

Link to comment
https://forums.phpfreaks.com/topic/154705-mysqli-and-exceptions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.