fork download
  1. <?php
  2.  
  3. class handler{
  4. /*
  5. Параметры подключения к БД
  6. */
  7. private $host="localhost";
  8. private $user="dk";
  9. private $database="data-control";
  10. private $pass="12345";
  11. private $charset="utf8";
  12. private $db;
  13. public $placeholders;
  14. public $values;
  15.  
  16. public function conn() {
  17. $this->db = new PDO("mysql:host=".$this->host.";dbname=".$this->database.";charset=".$this->charset, $this->user, $this->pass);
  18. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. }
  20.  
  21. // public function db_select($fields, $table, $fieldname=null, $id=null, $order=null)
  22. // {
  23. // $this->conn();
  24.  
  25. // if ($fields!="*") {
  26. // $fields=implode($fields,",");
  27. // }
  28.  
  29. // $sql = "SELECT $fields FROM `$table` ";
  30. // if ($fieldname!=null and $id!=null) $sql.=" WHERE `$fieldname`=:id";
  31. // if ($order!=null) $sql.=" ORDER BY :order";
  32.  
  33. // $stmt = $this->db->prepare($sql);
  34. // if ($id!=null) $stmt->bindParam('id', $id);
  35.  
  36. // if ($order!=null) $stmt->bindParam(':order',$order);
  37. // $stmt->execute();
  38.  
  39. // return $stmt->fetchAll(PDO::FETCH_CLASS,'person');
  40. // $conn=null;
  41. // }
  42.  
  43.  
  44. public function db_select_short($fields, $table, $fieldname=null, $id=null, $order=null)
  45. {
  46. $this->conn();
  47.  
  48. if ($fields!="*") {
  49. $fields=implode($fields,",");
  50. }
  51.  
  52. $sql = "SELECT $fields FROM `$table` ";
  53. if ($fieldname!=null and $id!=null) $sql.=" WHERE `$fieldname`=:id";
  54. if ($order!=null) $sql.=" ORDER BY :order";
  55.  
  56. $stmt = $this->db->prepare($sql);
  57. if ($id!=null) $stmt->bindParam('id', $id);
  58.  
  59. if ($order!=null) $stmt->bindParam(':order',$order);
  60. $stmt->execute();
  61.  
  62. $persons=array();
  63.  
  64. while ($row=$stmt->fetch(PDO::FETCH_OBJ)) {
  65. $persons[] = new person(
  66. $surname=$row->surname,
  67. $id=$row->id,
  68. $country=$row->country,
  69. $status=$row->display
  70. );
  71. }
  72.  
  73. return $persons;
  74. $conn=null;
  75. }
  76.  
  77. public function tab_columns ($table) {
  78. $this->conn();
  79. $sql="SELECT column_name, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table'";
  80. $stmt = $this->db->prepare($sql);
  81. $stmt->execute();
  82.  
  83. $stmt->setFetchMode(PDO::FETCH_ASSOC);
  84. $columns = $stmt->fetchAll(PDO::FETCH_UNIQUE); //какая-то магия(
  85. return $columns;
  86.  
  87. }
  88.  
  89. public function set_placeholders ($table,$data) {
  90. $columns=$this->tab_columns($table);
  91. $allowed=array_keys($columns);
  92. $placeholders = '';
  93. $values = array();
  94.  
  95. foreach ($allowed as $field) {
  96. if (isset($data->$field)) {
  97. $placeholders.="`".str_replace("`","``",$field)."`". "=:$field, ";
  98. $values[$field] =$data->$field;
  99. }
  100. }
  101.  
  102. $placeholders= substr($placeholders, 0, -2);
  103.  
  104. $this->placeholders=$placeholders;
  105. $this->values=$values;
  106. $conn=null;
  107. }
  108.  
  109. public function db_update($table, $data, $fieldname, $id)
  110. {
  111.  
  112. $this->set_placeholders ($table,$data);
  113. $sql = "UPDATE `$table` SET ".$this->placeholders." WHERE `$fieldname`=:id";
  114.  
  115. $stmt = $this->db->prepare($sql);
  116. if ($id!=null) $stmt->bindParam('id', $id);
  117.  
  118. $stmt->execute($this->values);
  119. return true;
  120. $conn=null;
  121. }
  122.  
  123. public function db_insert($table, $data)
  124. {
  125.  
  126. $this->set_placeholders ($table,$data);
  127. $sql = "INSERT INTO `$table` SET ".$this->placeholders;
  128.  
  129. $stmt = $this->db->prepare($sql);
  130. $stmt->execute($this->values);
  131. $lastId = $this->db->lastInsertId();
  132.  
  133. return $lastId;
  134. $conn=null;
  135. }
  136.  
  137. public function db_delete($table, $fieldname, $id)
  138. {
  139.  
  140. $sql = "DELETE FROM `$table` WHERE `$fieldname`=:id";
  141.  
  142. $stmt = $this->db->prepare($sql);
  143. if ($id!=null) $stmt->bindParam('id', $id);
  144. $stmt->execute();
  145.  
  146. return true;
  147. $conn=null;
  148. }
  149.  
  150. public function data_print ($data) {
  151. echo '<pre>';
  152. print_r($data);
  153. echo '</pre>';
  154. }
  155.  
  156.  
  157. }
  158.  
  159. class person {
  160. public $surname;
  161. public $id;
  162. public $country;
  163.  
  164. function __construct($surname,$id,$country,$display) {
  165. $this->surname=$surname;
  166. $this->id=$id;
  167. $this->country=$country;
  168. $this->status=$display;
  169. }
  170. }
  171.  
  172.  
  173. ?>
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Standard output is empty