Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Database Access Object
- class dao {
- private $db = NULL;
- private $connection_string = NULL;
- private $db_type = DB_TYPE;
- private $db_path = DB_PATH;
- private $db_host = DB_HOST;
- private $db_user = DB_USER;
- private $db_pass = DB_PASS;
- private $db_name = DB_NAME;
- private $con = false;
- public function __construct($db_user = DB_USER, $db_pass = DB_PASS, $db_name = DB_NAME, $db_type = 'mysql', $db_path = DB_PATH, $db_host = 'localhost')
- {
- $this->db_host = $db_host;
- $this->db_user = $db_user;
- $this->db_pass = $db_pass;
- $this->db_name = $db_name;
- $this->db_path = $db_path;
- $this->db_type = $db_type;
- switch($this->db_type){
- case "mysql":
- $this->connection_string = "mysql:host=".$db_host.";dbname=".$db_name;
- break;
- case "sqlite":
- $this->connection_string = "sqlite:".$db_path;
- break;
- case "oracle":
- $this->connection_string = "OCI:dbname=".$db_name.";charset=UTF-8";
- break;
- case "dblib":
- $this->connection_string = "dblib:host=".$db_host.";dbname=".$db_name;
- break;
- case "postgresql":
- $this->connection_string = "pgsql:host=".$db_host." dbname=".$db_name;
- break;
- }
- return $this;
- }
- public function connect() {
- if(!$this->con) {
- try {
- $this->db = new PDO($this->connection_string,$this->db_user, $this->db_pass);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->con = true;
- return $this->con;
- } catch (PDOException $e) {
- return $e->getMessage();
- }
- } else {
- return true;
- }
- }
- public function disconnect() {
- if($this->con) {
- unset($this->db);$this->con = false;
- return true;
- }
- }
- /**
- $db = new dao('username','password','database');
- $db->connect();
- if ($db->connect()) {
- echo "tersambung dengan database";
- } else {
- echo "gagal tersambung"
- $db->disconnect(); // kalau mau disconnect
- **/
- // SELECT
- public function select($table, $rows = '*', $where = null, $order = null) {
- if($this->tableExists($table)) {
- $q = 'SELECT '.$rows.' FROM '.$table;
- if($where != null)
- $q .= ' WHERE '.$where;
- if($order != null)
- $q .= ' ORDER BY '.$order;
- $this->numResults = null;
- try {
- $sql = $this->db->prepare($q);
- $sql->execute();
- $this->result = $sql->fetchAll(PDO::FETCH_ASSOC);
- $this->numResults = count($this->result);
- $this->numResults === 0 ? $this->result = null : true ;
- return true;
- } catch (PDOException $e) {
- return $e->getMessage().''.$e->getTraceAsString().'';
- }
- }
- }
- public function getResult(){
- return $this->result;
- }
- /**
- $result = $db->select('users');
- if ($result === true){
- echo "result select ";
- print_r($db->getResult());
- } else {
- var_dump($result);
- }
- **/
- // INSERT
- public function insert ($table,$values,$rows = null) {
- $insert = 'INSERT INTO '.$table;
- if($rows != null) {
- $insert .= ' ('.$rows.')';
- }
- for($i = 0; $i < count($values); $i++) {
- if(is_string($values[$i]))
- $values[$i] = '"'.$values[$i].'"';
- }
- $values = implode(',',$values);
- $insert .= ' VALUES ('.$values.')';
- try {
- $ins = $this->db->prepare($insert);
- $ins->execute();
- return true;
- } catch (PDOException $e) {
- return $e->getMessage();
- }
- }
- /**
- $result = $db->insert("user", array("","test123","test","0"), "id,username,password,active");
- **/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement