table.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. require_once "../redis/function.php";
  3. // Require Composer's autoloader.
  4. //require '../../vendor/autoload.php';
  5. // Using Medoo namespace.
  6. //use Medoo\Medoo;
  7. class Table
  8. {
  9. protected $dbh;
  10. protected $table;
  11. protected $redis;
  12. protected $errorMessage;
  13. protected $field_setting;
  14. protected $result;
  15. protected $medoo;
  16. protected $redisProfix;
  17. function __construct($db,$table,$user="",$password="",$redis=false) {
  18. $this->dbh = new PDO($db, $user, $password,array(PDO::ATTR_PERSISTENT=>true));
  19. $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  20. /*
  21. $database = new Medoo([
  22. // Initialized and connected PDO object.
  23. 'pdo' => $this->dbh,
  24. // [optional] Medoo will have different handle method according to different database type.
  25. 'type' => 'sqlite'
  26. ]);
  27. $this->medoo = $database;
  28. */
  29. $this->redis = $redis;
  30. $this->table = $table;
  31. $this->result = ["ok"=>true,"message"=>"","data"=>array()];
  32. $this->redisProfix = $table . "/:id";
  33. }
  34. /*
  35. public function index($columns,$where){
  36. $output = $this->medoo->select(
  37. $this->table,
  38. $columns,
  39. $where
  40. );
  41. $this->result["data"] = $output;
  42. return $this->result;
  43. }
  44. public function create($data,$columns){
  45. foreach ($columns as $value) {
  46. # code...
  47. $updateDate[$value] = $data[$value];
  48. }
  49. $this->medoo->insert(
  50. $this->table,
  51. $updateDate
  52. );
  53. $newId = $database->id;
  54. $this->result["data"] = $newId;
  55. return $this->result;
  56. }
  57. public function update($data,$columns,$where=null){
  58. foreach ($columns as $value) {
  59. # code...
  60. $updateDate[$value] = $data[$value];
  61. }
  62. if($where==null){
  63. $where = ["id"=>$data["id"]];
  64. }
  65. $this->medoo->update(
  66. $this->table,
  67. $updateDate,
  68. $where
  69. );
  70. return $this->result;
  71. }
  72. public function get($columns,$where){
  73. $output = $this->medoo->get(
  74. $this->table,
  75. $columns,
  76. $where
  77. );
  78. $this->result["data"] = $output;
  79. return $this->result;
  80. }
  81. public function delete($where){
  82. $output = $this->medoo->delete(
  83. $this->table,
  84. $where
  85. );
  86. $this->result["data"] = $output->rowCount();
  87. return $this->result;
  88. }
  89. */
  90. protected function fetch($query,$params){
  91. if (isset($params)) {
  92. $stmt = $this->dbh->prepare($query);
  93. if($stmt){
  94. $stmt->execute($params);
  95. }
  96. } else {
  97. $stmt = $PDO->query($query);
  98. }
  99. if($stmt){
  100. return $stmt->fetch(PDO::FETCH_ASSOC);
  101. }
  102. else{
  103. return false;
  104. }
  105. }
  106. function execute($query, $params=null){
  107. if (isset($params)) {
  108. $stmt = $this->dbh->prepare($query);
  109. if($stmt){
  110. $stmt->execute($params);
  111. return $stmt;
  112. }
  113. else{
  114. return false;
  115. }
  116. } else {
  117. return $this->dbh->query($query);
  118. }
  119. }
  120. }
  121. ?>