0

can anyone tell me why I'm getting Connection error: SQLSTATE[HY000] [1049] Unknown database 'api_db' error in my browser? My DB credentials are correct and the DB does indeed exist. I've tried changing my host to localhost but if I do that, I get Connection error: SQLSTATE[HY000] [2002] No such file or directory error.

What am I doing wrong?

Here's my code:

<?php

class Database { 

private $host = "127.0.01";
private $db_name = "api_db";
private $username = "root";
private $password = "root";
public $conn;

// get the database connection
public function getConnection(){

    $this->conn = null;

    try{
        $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        $this->conn->exec("set names utf8");
    }catch(PDOException $exception){
        echo "Connection error: " . $exception->getMessage();
    }

    return $this->conn;
  }
}
?>
user3783243
  • 5,368
  • 5
  • 22
  • 41
cloudyday
  • 183
  • 6

0 Answers0