Error with pdo connection

0

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;

    }

}
    
asked by kmilo93sd 12.03.2018 в 04:23
source

1 answer

0

Use these codes:

Database.php file

<?php
class Database
{
    public static function StartUp()
    {
        $pdo = new PDO('mysql:host=host;dbname=basededatos;charset=utf8', 'usuario', 'contraseña');
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
        return $pdo;
    }
}

Model File:

<?php
require_once 'model/database.php';
class Model
{
    private $pdo;

    public function __CONSTRUCT()
    {
        try
        {
            $this->pdo = Database::StartUp();     
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }

    public function getAll()
    {
        try
        {
            $result = array();

            $stm = $this->pdo->prepare("SELECT * FROM tabla_de_basedatos");
            $stm->execute();

            return $stm->fetchAll(PDO::FETCH_OBJ);
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }
}

The getAll () method returns all the records that you can list with a foreach.

    
answered by 12.03.2018 в 04:42