Guest User

Untitled

a guest
Mar 11th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. Uncaught Error: Call to undefined method Connection::prepare() in C:xampphtdocsrank-APIappmodelsModel.php:17
  2.  
  3. class Model
  4. {
  5. protected $table = "default";
  6. protected $primaryKey = "id";
  7. protected $connection;
  8.  
  9. function __construct()
  10. {
  11. $this->connection = Connection::getConnection();
  12. }
  13.  
  14. public function getAll(){
  15. try{
  16.  
  17. $query = $this->connection->prepare('select * from ' . $this->table);
  18. $query->execute();
  19.  
  20. return $query->fetchAll();
  21.  
  22. }catch(PDOException $e){
  23.  
  24. echo $e->getMessage();
  25.  
  26. }finally{
  27.  
  28. $this->connection = null;
  29.  
  30. }
  31.  
  32. }
  33.  
  34. }
  35.  
  36. class Connection
  37. {
  38. private $host = DB_CONFIG['host'];
  39. private $database = DB_CONFIG['database'];
  40. private $user = DB_CONFIG['user'];
  41. private $pass = DB_CONFIG['pass'];
  42.  
  43. private static $connection;
  44.  
  45.  
  46. function __construct()
  47. {
  48. try{
  49.  
  50. $connection = new PDO("mysql:host=$this->host;dbname=$this->database",
  51. $this->user,
  52. $this->pass);
  53. $connection->exec("set names utf8");
  54. $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  55.  
  56. self::$connection = $connection;
  57.  
  58.  
  59. }
  60. catch (PDOException $e) {
  61.  
  62. echo 'Connection failed: ' . $e->getMessage();
  63.  
  64. }
  65. }
  66.  
  67. public static function getConnection(){
  68.  
  69. if (self::$connection === null) {
  70. self::$connection = new self();
  71. }
  72. return self::$connection;
  73.  
  74. }
  75.  
  76. }
Add Comment
Please, Sign In to add comment