Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AcessDataPDO{
- private $driver;
- private $user;
- private $password;
- private $option;
- protected $PDOconnection;
- private function beginConnection(){
- try {
- $this->PDOconnection = new PDO($this->driver, $this->user, $this->password, $this->option);
- } catch (Exception $e) {
- $tag = ' <script type="text/javascript">';
- $tag += "alert(\"";
- $tag += $e->getFile()." - Na Linha ".$e->getLine()." # ".$e->getMessage()."\")";
- $tag += '</script>';
- echo $tag;
- }
- }
- public function __construct(){
- require_once 'Configurador.class.php';
- $conf = new Configurador();
- $this->driver = $conf->driver;
- $this->user = $conf->user;
- $this->password = $conf->password;
- $this->option = $conf->option;
- $this->beginConnection();
- }
- public function __construct($driver,$user,$password,$option){
- $this->driver = $driver;
- $this->user = $user;
- $this->password = $password;
- $this->option = $option;
- $this->beginConnection();
- }
- private function query($type, $expression){
- if( $type == "INSERT"){
- if ($this->PDOconnection->query($expression)){
- return true;
- }else{
- return false;
- }
- }elseif ( $type == "UPDATE"){
- if ($this->PDOconnection->query($expression)){
- return true;
- }else{
- return false;
- }
- }elseif ($type == "DELETE"){
- if ($this->PDOconnection->query($expression)){
- return true;
- }else{
- return false;
- }
- }elseif ($type == "SELECT"){
- if ($resultSet = $this->PDOconnection->query($expression)){
- $answer = $resultSet->fetchObject();
- return $answer;
- }else{
- return false;
- }
- }
- }
- public function select($columns,$table,$condition){
- $expression = "SELECT $coulmns FROM $table ";
- if($condition != null){
- $expression += "WHERE $condition";
- }
- return $this->query("SELECT", $expression);
- }
- public function insert($table,$values,$condition){
- $expression = "INSERT INTO $table VALUES ($values) ";
- if($condition != null){
- $expression += "WHERE $condition";
- }
- return $this->query("INSERT", $expression);
- }
- /**
- *
- * Enter description here ...
- * @param $columnsSet - coluna tem de ser um array
- * @param $table
- * @param $condition
- */
- public function update($columnsSet,$table,$condition){
- if(is_array($columnsSet)){
- $expression = "UPDATE $table SET";
- foreach ($columnsSet as $k => $v)
- $expression += " $k = $v";
- if($condition != null){
- $expression += "WHERE $condition";
- }
- return $this->query("UPDATE", $expression);
- }
- return false;
- }
- public function delete($table,$condition){
- $expression = "DELETE FROM $table ";
- if($condition != null){
- $expression += "WHERE $condition";
- }
- return $this->query("DELETE", $expression);
- }
- private function __destruct(){
- $this->PDOconnection = null;
- }
- }
- eu li alguns tutoriais e visando as minhas necessidades pra criar um projeto escolar simples!
- A pergunta q eu faço é se o fato se essa classe é mais "inteligente" ou profissional do que
- a seguinte classe!
- <?php
- class AcessDataPDO{
- private $driver;
- private $host;
- private $user;
- private $password;
- private $dataBase;
- private $option;
- private $port;
- protected $PDOconnection;
- private function beginConnection(){
- try {
- $this->PDOconnection = new PDO($this->driver, $this->user, $this->password, $this->option);
- } catch (Exception $e) {
- $tag = ' <script type="text/javascript">';
- $tag += "alert(\"";
- $tag += $e->getFile()." - Na Linha ".$e->getLine()." # ".$e->getMessage()."\")";
- $tag += '</script>';
- echo $tag;
- }
- }
- public function __construct(){
- require_once 'Configurador.class.php';
- $conf = new Configurador();
- $this->driver = $conf->driver;
- $this->user = $conf->user;
- $this->password = $conf->password;
- $this->option = $conf->option;
- $this->beginConnection();
- }
- public function __construct($driver,$user,$password,$option){
- $this->driver = $driver;
- $this->user = $user;
- $this->password = $password;
- $this->option = $option;
- $this->beginConnection();
- }
- public function query($type, $expression){
- if( $type == "INSERT"){
- if ($this->PDOconnection->query($expression)){
- return true;
- }else{
- return false;
- }
- }elseif ( $type == "UPDATE"){
- if ($this->PDOconnection->query($expression)){
- return true;
- }else{
- return false;
- }
- }elseif ($type == "DELETE"){
- if ($this->PDOconnection->query($expression)){
- return true;
- }else{
- return false;
- }
- }elseif ($type == "SELECT"){
- if ($resultSet = $this->PDOconnection->query($expression)){
- $answer = $resultSet->fetchObject();
- return $answer;
- }else{
- return false;
- }
- }
- }
- private function __destruct(){
- $this->PDOconnection = null;
- }
- }
Add Comment
Please, Sign In to add comment