I'm trying to echo out data from my database through my Retrieve Class using PDO. I'm having trouble doing that. Can someone please help me?
This error shows:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Retrieve::__construct(), 0 passed in index.php on line 89 and exactly 1 expected in index.php:58 Stack trace: #0 index.php(89): Retrieve->__construct() #1 {main} thrown in index.php on line 58
This is my code:
<?php
class Database {
private $host = 'localhost';
private $db_name = 'photos';
private $username = 'root';
private $password = '';
private $conn;
public function connect() {
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);
echo "Connected successfully";
} catch(PDOException $e) {
echo 'Connection Error: ' . $e->getMessage();
}
$this->conn = null;
}
}
class Retrieve extends Database {
private $conn;
private $table = 'indeximg';
public $id;
public $username;
public $img;
public function __construct($db) {
$this->conn = $db;
}
public function read() {
$query = 'SELECT id, username, img FROM' . $this->table;
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
}
$object = new Retrieve;
echo $object->read();
?>