-
-
Save mateuspontes/809397 to your computer and use it in GitHub Desktop.
Db PDO #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class Db extends PDO { | |
public static $_instance = NULL; | |
public static function getInstance() { | |
if (NULL === self::$_instance) { | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
parent::__construct('pgsql:host=127.0.0.1;dbname=foo','user','senha'); | |
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
} | |
public function fetchRow($query) { | |
$sql = parent::query($query); | |
return $sql->fetch(); | |
} | |
public function fetchOne($query) { | |
if ($curr = $this->fetchRow($query)) | |
return current($curr); | |
return false; | |
} | |
public function update(array $fields,$where) { | |
$set = array(); | |
foreach ($fields as $f => $v) | |
$set[] = "{$f} = {$v}"; | |
return parent::exec("UPDATE {$this->_table} SET ".join(',',$set)." WHERE {$where}"); | |
} | |
public function insert(array $array) { | |
return parent::exec("INSERT INTO {$this->_table}(".join(',',array_keys($array)).") VALUES(".join(',',array_values($array)).")"); | |
} | |
public function delete($id) { | |
return parent::exec("DELETE FROM {$this->_table} WHERE {$this->_primary} = {$id}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment