Guest User

Untitled

a guest
May 29th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class PDODO
  5. {
  6. private $db, $table_name;
  7.  
  8. /**
  9. *
  10. * @Connect to the database and set the error mode to Exception
  11. *
  12. * @Throws PDOException on failure
  13. *
  14. */
  15. public function conn()
  16. {
  17. isset($this->username);
  18. isset($this->password);
  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->table_name = get_called_class();
  35. }
  36.  
  37.  
  38. /**
  39. * settor
  40. *
  41. * @access public
  42. *
  43. * @param string $name
  44. *
  45. * @param string $value
  46. *
  47. */
  48. public function __set($name, $value)
  49. {
  50. switch($name)
  51. {
  52. case 'dsn':
  53. $this->dsn = $value;
  54. break;
  55.  
  56. case 'username':
  57. $this->username = $value;
  58. break;
  59.  
  60. case 'password':
  61. $this->password = $value;
  62. break;
  63.  
  64. case 'field_list':
  65. if(is_array($value))
  66. {
  67. $this->field_list = $value;
  68. }
  69. break;
  70.  
  71. default:
  72. throw new Exception ("Uable to set $name");
  73. }
  74. }
  75.  
  76.  
  77.  
  78. /***
  79. *
  80. * @select values from table
  81. *
  82. * @access public
  83. *
  84. * @param string $table The name of the table
  85. *
  86. * @param string $fieldname
  87. *
  88. * @param string $id
  89. *
  90. * @return array on success or throw PDOException on failure
  91. *
  92. */
  93. public function selectAll()
  94. {
  95. $this->conn();
  96. $sql = "SELECT * FROM `$this->table_name`";
  97. $stmt = $this->db->prepare($sql);
  98. $stmt->bindParam(':id', $id);
  99. $stmt->execute();
  100. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  101. }
  102.  
  103.  
  104. // insert something
  105. function insert($fieldarray)
  106. {
  107. $this->errors = array();
  108.  
  109. $field_list = $this->field_list;
  110. foreach ($fieldarray as $field => $fieldvalue)
  111. {
  112. if (!in_array($field, $field_list))
  113. {
  114. unset ($fieldarray[$field]);
  115. } // if
  116. } // foreach
  117.  
  118. // We can now construct the query string to insert a new record into the database:
  119. $query = "INSERT INTO $this->tablename SET ";
  120. foreach ($fieldarray as $item => $value)
  121. {
  122. $query .= "$item='$value', ";
  123. }
  124. // remove trailing comma (,)
  125. $query = rtrim($query, ', ');
  126.  
  127. $this->conn->query($query);
  128. }
  129.  
  130.  
  131.  
  132. } // end of class
  133.  
  134.  
  135. /**
  136. *
  137. * @class name is named the same as the db table
  138. *
  139. */
  140. class cars extends PDODO
  141. {
  142. // additional class variables go here
  143. public function __construct()
  144. {
  145. $this->dsn = "mysql:dbname=test;host=localhost";
  146. $this->username = 'username';
  147. $this->password = 'password';
  148.  
  149. $this->field_list = array('car_id', 'car_cost', 'car_price', 'car_description');
  150. parent::__construct();
  151. }
  152.  
  153. /**
  154. * Call parent methods
  155. *
  156. * @param string $name The name of the parent method
  157. *
  158. * @param array $args An array of args for the parent method
  159. *
  160. */
  161. public function __call($name, $args=null)
  162. {
  163. return parent::$name();
  164. }
  165. } // end class
  166.  
  167.  
  168. $cars = new cars;
  169. $result = $cars->selectAll();
  170.  
  171. print_r($result);
  172.  
  173. ?>
Add Comment
Please, Sign In to add comment