Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- class PDODO
- {
- protected $table_name, $where;
- protected $db;
- /**
- *
- * @Connect to the database and set the error mode to Exception
- *
- * @Throws PDOException on failure
- *
- */
- protected function conn()
- {
- if (!$this->db instanceof PDO)
- {
- $this->db = new PDO($this->dsn, $this->username, $this->password);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- }
- /**
- *
- * The constructor each derived class should have
- * its own constructor containing proper values
- *
- */
- function __construct()
- {
- $this->dsn = "mysql:dbname=test;host=localhost";
- $this->username = 'username';
- $this->password = 'password';
- $this->table_name = get_called_class();
- }
- /**
- * settor
- *
- * @access public
- *
- * @param string $name
- *
- * @param string $value
- *
- */
- public function __set($name, $value)
- {
- switch($name)
- {
- case 'dsn':
- $this->dsn = $value;
- break;
- case 'username':
- $this->username = $value;
- break;
- case 'password':
- $this->password = $value;
- break;
- case 'primary_key':
- $this->primary_key = $value;
- break;
- default:
- throw new Exception ("Uable to set $name");
- }
- }
- /***
- *
- * @find value(s) from table
- *
- * @access public
- *
- * @param string $id
- *
- * @return array on success or throw PDOException on failure
- *
- */
- public function find($id=null)
- {
- $this->conn();
- $sql = "SELECT * FROM $this->table_name";
- $sql .= is_null($id) ? '' : " WHERE $this->primary_key=:id";
- $stmt = $this->db->prepare($sql);
- $stmt->bindParam(':id', $id);
- $stmt->execute();
- return $stmt->fetchAll(PDO::FETCH_OBJ);
- }
- /*
- *
- * INSERT record or records
- *
- * @access public
- *
- * @param array $values
- *
- * @throws PDOException on failure
- *
- * @return int Last Insert ID
- *
- */
- public function insert($values)
- {
- /*** get the field list ***/
- $fieldnames = array_keys($values[0]);
- $this->conn();
- /*** now the query ***/
- $sql = "INSERT INTO $this->table_name ";
- /*** set the field names ***/
- $fields = '( ' . implode(', ', $fieldnames) . ' )';
- /*** set the placeholders ***/
- $sql .= $fields.' VALUES ';
- $i=0; $size = sizeof($values);
- foreach($values as $vals)
- {
- $sql .= "('" . implode("', '", $vals) . "')";
- $sql .= ($i<($size-1)) ? ',' : '';
- $i++;
- }
- $this->db->quote($sql);
- $this->db->query($sql);
- return $this->db->lastInsertId();
- }
- /*
- *
- * @execute a raw SQL query
- *
- * @access protected
- *
- * @param string $sql
- *
- * @return array
- *
- */
- public function rawFind($sql)
- {
- $stmt = $this->db->prepare($sql);
- $stmt->execute();
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- public function update($id, $values)
- {
- }
- public function delete()
- {
- }
- } // end of class
- /**
- *
- * @class name is named the same as the db table
- *
- */
- class cars extends PDODO
- {
- protected $car_id, $car_cost, $car_price, $car_description;
- protected $primary_key = 'car_id';
- protected function conn()
- {
- $this->db = new PDO("mysql:host=localhost;dbname=test", 'username', 'password');
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- } // end class
- $cars = new cars;
- /*** insert a new record ***/
- $car1 = array ('car_cost'=>2000, 'car_price'=>4999, 'car_description'=>'New Car');
- $car2 = array ('car_cost'=>200, 'car_price'=>400, 'car_description'=>'Old POS');
- $car3 = array ('car_cost'=>20000, 'car_price'=>40000, 'car_description'=>'Fast Car');
- // $last_id = $cars->insert(array($car1, $car2, $car3));
- // echo $last_id;
- $res = $cars->find(23);
- echo $res[0]->car_cost;
- ?>
Add Comment
Please, Sign In to add comment