Home » Php » php – Connection error: SQLSTATE[HY000 ] [ 1049 ] Unknown database 'student'

php – Connection error: SQLSTATE[HY000 ] [ 1049 ] Unknown database 'student'

Posted by: admin February 25, 2020 Leave a comment

Questions:
public $host = "localhost";
public $db_name = "student";
public $username = "root";
private $password = "";
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);    
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}

Fatal error: Uncaught Error: Call to a member function prepare() on null

How to&Answers: