Advertisement
Guest User

Untitled

a guest
Oct 30th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. Job.php
  2.  
  3.  
  4. <?php
  5. class Job{
  6. private $db;
  7.  
  8. public function __construct(){
  9. $this->db = new Database;
  10. }
  11.  
  12. //Get all jobs
  13. public function getAllJobs(){
  14. $this->db->query("SELECT jobs.*, categories.name AS cname FROM jobs
  15. INNER JOIN categories
  16. ON jobs.category_id = categories.id
  17. ORDER BY post_date DESC
  18. ");
  19. //assign result set
  20. $results = $this->db->resultSet();
  21.  
  22. return $results;
  23. }
  24. }
  25. ?>
  26.  
  27.  
  28. Database.php<?php
  29. class Database{
  30. private $host = DB_HOST;
  31. private $user = DB_USER;
  32. private $pass = DB_PASS;
  33. private $dbname = DB_NAME;
  34.  
  35. private $dbh;
  36. private $err;
  37. private $stmt;
  38.  
  39. public function __construct(){
  40. //Set DSN
  41. $dsn = 'mysql:host' . $this->host . ';dbname=' . $this->dbname;
  42.  
  43. //set options
  44. $options = array(
  45. PDO::ATTR_PERSISTENT => true,
  46. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  47. );
  48.  
  49. //PDO Instance
  50.  
  51. try{
  52. $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  53. }catch(PDOException $e) {
  54. $this->error = $e->getMessage();
  55. }
  56. }
  57.  
  58. public function query($query){
  59. $this->stmt = $this->dbh->prepare($query);
  60. }
  61.  
  62. public function bind($param,$value,$type = null){
  63. if(is_null($type)){
  64. switch(true){
  65. case is_int ($value):
  66. $type = PDO::PARAM_INT;
  67. break;
  68.  
  69. case is_bool ($value) :
  70. $type = PDO::PARAM_BOOL;
  71. break;
  72.  
  73. case is_null ($value):
  74. $type = PDO::PARAM_NULL;
  75. break;
  76. default:
  77. $type = PDO::PARAM_STR;
  78. }
  79. }
  80. $this->stmt->bindValue($param,$value,$type);
  81. }
  82.  
  83. public function execute(){
  84. return $this->stmt->execute();
  85. }
  86.  
  87. public function resultSet(){
  88. $this->execute();
  89. return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  90. }
  91.  
  92. public function single(){
  93. $this->execute();
  94. return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement