I'm trying to access the database using the pdo connection, I'm doing it using a Model class that contains the connection and a query that is inherited by the User class that is the one I'm calling. But it throws this error when using the method getAll ()
Uncaught Error: Call to undefined method Connection::prepare() in C:\xampp\htdocs\rank-API\app\models\Model.php:17
these are the model and connection classes, the user only overwrites the table attribute
model class:
class Model
{
protected $table = "default";
protected $primaryKey = "id";
protected $connection;
function __construct()
{
$this->connection = Connection::getConnection();
}
public function getAll(){
try{
$query = $this->connection->prepare('select * from ' . $this->table);
$query->execute();
return $query->fetchAll();
}catch(PDOException $e){
echo $e->getMessage();
}finally{
$this->connection = null;
}
}
}
connection class:
class Connection
{
private $host = DB_CONFIG['host'];
private $database = DB_CONFIG['database'];
private $user = DB_CONFIG['user'];
private $pass = DB_CONFIG['pass'];
private static $connection;
function __construct()
{
try{
$connection = new PDO("mysql:host=$this->host;dbname=$this->database",
$this->user,
$this->pass);
$connection->exec("set names utf8");
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$connection = $connection;
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public static function getConnection(){
if (self::$connection === null) {
self::$connection = new self();
}
return self::$connection;
}
}