Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class PDODO
- {
- private $db, $table_name;
- /**
- *
- * @Connect to the database and set the error mode to Exception
- *
- * @Throws PDOException on failure
- *
- */
- public function conn()
- {
- isset($this->username);
- isset($this->password);
- 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->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 'field_list':
- if(is_array($value))
- {
- $this->field_list = $value;
- }
- break;
- default:
- throw new Exception ("Uable to set $name");
- }
- }
- /***
- *
- * @select values from table
- *
- * @access public
- *
- * @param string $table The name of the table
- *
- * @param string $fieldname
- *
- * @param string $id
- *
- * @return array on success or throw PDOException on failure
- *
- */
- public function selectAll()
- {
- $this->conn();
- $sql = "SELECT * FROM `$this->table_name`";
- $stmt = $this->db->prepare($sql);
- $stmt->bindParam(':id', $id);
- $stmt->execute();
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
- // insert something
- function insert($fieldarray)
- {
- $this->errors = array();
- $field_list = $this->field_list;
- foreach ($fieldarray as $field => $fieldvalue)
- {
- if (!in_array($field, $field_list))
- {
- unset ($fieldarray[$field]);
- } // if
- } // foreach
- // We can now construct the query string to insert a new record into the database:
- $query = "INSERT INTO $this->tablename SET ";
- foreach ($fieldarray as $item => $value)
- {
- $query .= "$item='$value', ";
- }
- // remove trailing comma (,)
- $query = rtrim($query, ', ');
- $this->conn->query($query);
- }
- } // end of class
- /**
- *
- * @class name is named the same as the db table
- *
- */
- class cars extends PDODO
- {
- // additional class variables go here
- public function __construct()
- {
- $this->dsn = "mysql:dbname=test;host=localhost";
- $this->username = 'username';
- $this->password = 'password';
- $this->field_list = array('car_id', 'car_cost', 'car_price', 'car_description');
- parent::__construct();
- }
- /**
- * Call parent methods
- *
- * @param string $name The name of the parent method
- *
- * @param array $args An array of args for the parent method
- *
- */
- public function __call($name, $args=null)
- {
- return parent::$name();
- }
- } // end class
- $cars = new cars;
- $result = $cars->selectAll();
- print_r($result);
- ?>
Add Comment
Please, Sign In to add comment