Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Uncaught Error: Call to undefined method Connection::prepare() in C:xampphtdocsrank-APIappmodelsModel.php:17
- 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;
- }
- }
- }
- 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;
- }
- }
Add Comment
Please, Sign In to add comment