Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.