Guest User

Untitled

a guest
May 29th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4.  
  5. class PDODO
  6. {
  7. protected $table_name, $where;
  8. protected $db;
  9.  
  10. /**
  11. *
  12. * @Connect to the database and set the error mode to Exception
  13. *
  14. * @Throws PDOException on failure
  15. *
  16. */
  17. protected function conn()
  18. {
  19. if (!$this->db instanceof PDO)
  20. {
  21. $this->db = new PDO($this->dsn, $this->username, $this->password);
  22. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  23. }
  24. }
  25.  
  26. /**
  27. *
  28. * The constructor each derived class should have
  29. * its own constructor containing proper values
  30. *
  31. */
  32. function __construct()
  33. {
  34. $this->dsn = "mysql:dbname=test;host=localhost";
  35. $this->username = 'username';
  36. $this->password = 'password';
  37.  
  38. $this->table_name = get_called_class();
  39. }
  40.  
  41.  
  42. /**
  43. * settor
  44. *
  45. * @access public
  46. *
  47. * @param string $name
  48. *
  49. * @param string $value
  50. *
  51. */
  52. public function __set($name, $value)
  53. {
  54. switch($name)
  55. {
  56. case 'dsn':
  57. $this->dsn = $value;
  58. break;
  59.  
  60. case 'username':
  61. $this->username = $value;
  62. break;
  63.  
  64. case 'password':
  65. $this->password = $value;
  66. break;
  67.  
  68. case 'primary_key':
  69. $this->primary_key = $value;
  70. break;
  71.  
  72. default:
  73. throw new Exception ("Uable to set $name");
  74. }
  75. }
  76.  
  77.  
  78.  
  79. /***
  80. *
  81. * @find value(s) from table
  82. *
  83. * @access public
  84. *
  85. * @param string $id
  86. *
  87. * @return array on success or throw PDOException on failure
  88. *
  89. */
  90. public function find($id=null)
  91. {
  92. $this->conn();
  93. $sql = "SELECT * FROM $this->table_name";
  94. $sql .= is_null($id) ? '' : " WHERE $this->primary_key=:id";
  95. $stmt = $this->db->prepare($sql);
  96. $stmt->bindParam(':id', $id);
  97. $stmt->execute();
  98. return $stmt->fetchAll(PDO::FETCH_OBJ);
  99. }
  100.  
  101.  
  102.  
  103. /*
  104. *
  105. * INSERT record or records
  106. *
  107. * @access public
  108. *
  109. * @param array $values
  110. *
  111. * @throws PDOException on failure
  112. *
  113. * @return int Last Insert ID
  114. *
  115. */
  116. public function insert($values)
  117. {
  118. /*** get the field list ***/
  119. $fieldnames = array_keys($values[0]);
  120.  
  121. $this->conn();
  122. /*** now the query ***/
  123. $sql = "INSERT INTO $this->table_name ";
  124. /*** set the field names ***/
  125. $fields = '( ' . implode(', ', $fieldnames) . ' )';
  126. /*** set the placeholders ***/
  127. $sql .= $fields.' VALUES ';
  128. $i=0; $size = sizeof($values);
  129. foreach($values as $vals)
  130. {
  131. $sql .= "('" . implode("', '", $vals) . "')";
  132. $sql .= ($i<($size-1)) ? ',' : '';
  133. $i++;
  134. }
  135. $this->db->quote($sql);
  136. $this->db->query($sql);
  137. return $this->db->lastInsertId();
  138. }
  139.  
  140.  
  141. /*
  142. *
  143. * @execute a raw SQL query
  144. *
  145. * @access protected
  146. *
  147. * @param string $sql
  148. *
  149. * @return array
  150. *
  151. */
  152. public function rawFind($sql)
  153. {
  154. $stmt = $this->db->prepare($sql);
  155. $stmt->execute();
  156. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  157. }
  158.  
  159.  
  160. public function update($id, $values)
  161. {
  162. }
  163.  
  164.  
  165. public function delete()
  166. {
  167. }
  168.  
  169. } // end of class
  170.  
  171.  
  172. /**
  173. *
  174. * @class name is named the same as the db table
  175. *
  176. */
  177. class cars extends PDODO
  178. {
  179. protected $car_id, $car_cost, $car_price, $car_description;
  180. protected $primary_key = 'car_id';
  181.  
  182. protected function conn()
  183. {
  184. $this->db = new PDO("mysql:host=localhost;dbname=test", 'username', 'password');
  185. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  186. }
  187.  
  188.  
  189. } // end class
  190.  
  191.  
  192. $cars = new cars;
  193.  
  194. /*** insert a new record ***/
  195. $car1 = array ('car_cost'=>2000, 'car_price'=>4999, 'car_description'=>'New Car');
  196. $car2 = array ('car_cost'=>200, 'car_price'=>400, 'car_description'=>'Old POS');
  197. $car3 = array ('car_cost'=>20000, 'car_price'=>40000, 'car_description'=>'Fast Car');
  198.  
  199. // $last_id = $cars->insert(array($car1, $car2, $car3));
  200. // echo $last_id;
  201.  
  202. $res = $cars->find(23);
  203. echo $res[0]->car_cost;
  204.  
  205. ?>
Add Comment
Please, Sign In to add comment