This is my code:
<?php
class Database {
private $host = 'localhost';
private $db_name = 'myblog';
private $username = 'root';
private $password = '';
private $conn;
public function connect() {
$this->conn = null;
try {
$this->conn = new PDO('mysql:host=' . $this->host . '.dbname=' . $this->db_name,
$this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'Connection Error:' .$e->getMessage();
}
return $this->conn;
}
}
And this what did they say
Connection Error:SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for localhost.dbname=myblog failed: No such host is known. <br />
<b>Fatal error</b>: Uncaught Error: Call to a member function prepare() on null in C:\Users\agadir\Downloads\xampp3\htdocs\API project\Models\post.php:38
Stack trace:
#0 C:\Users\agadir\Downloads\xampp3\htdocs\API project\API\POST\read.php(18): Post->read()
#1 {main}
thrown in <b>C:\Users\agadir\Downloads\xampp3\htdocs\API project\Models\post.php</b> on line <b>38</b><br />