Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.03 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Conn.class [ CONEXÃO ]
  5.  * Classe abstrata de conexão. Padrão SingleTon.
  6.  * Retorna um objeto PDO pelo método estático getConn();
  7.  *
  8.  * @copyright (60272)(c) 2016, Robson V. Leite - UPINSIDE TREINAMENTOS
  9.  */
  10. class Conn {
  11.  
  12.     private static $Host = HOST;
  13.     private static $User = USER;
  14.     private static $Pass = PASS;
  15.     private static $Dbsa = DBSA;
  16.  
  17.     /** @var PDO */
  18.     private static $Connect = null;
  19.  
  20.     /**
  21.      * Conecta com o banco de dados com o pattern singleton.
  22.      * Retorna um objeto PDO!
  23.      */
  24.     private static function Conectar() {
  25.         try {
  26.             if (self::$Connect == null):
  27.                 $dsn = 'mysql:host=' . self::$Host . ';dbname=' . self::$Dbsa;
  28.                 $options = [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'];
  29.                 self::$Connect = new PDO($dsn, self::$User, self::$Pass, $options);
  30.             endif;
  31.         } catch (PDOException $e) {
  32.             PHPErro($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
  33.             die;
  34.         }
  35.  
  36.         self::$Connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  37.         return self::$Connect;
  38.     }
  39.  
  40.     /** Retorna um objeto PDO Singleton Pattern. */
  41.     protected static function getConn() {
  42.         return self::Conectar();
  43.     }
  44.  
  45. }
  46.  
  47.  
  48. <!DOCTYPE html>
  49. <html lang="pt-br">
  50.     <head>
  51.         <meta charset="UTF-8">
  52.         <title>Prepared Statements</title>
  53.     </head>
  54.     <body>
  55.         <?php
  56.         require('../07-php-data-object/_app/Config.inc.php');
  57.        
  58.         //nossa classe de conexão
  59.         $PDO = new Conn;
  60.         $name = "Firefox";
  61.         $views = '128';
  62.        
  63.         //vamos manipular a tabela ws_siteviews_agent
  64.         try{
  65.            
  66.             //cadastro no banco ---> quando passamos ? nos inserts,
  67.             //é como se tivessemos informando que vão ser passados para a query
  68.             $QRCreate = "INSERT INTO ws_siteviews_agent (agent_name, agent_views) VALUES (?, ?)";
  69.            
  70.             $Create = $PDO->getConn()->prepare($QRCreate);
  71.            
  72.            
  73.             //informar a inserção de dados. O 3o parâmetro não é obrigatório,
  74.             //o bind value reconhece o conteúdo sempre como uma string. Se não
  75.             //for informado e passarmos um INT no lugar do chrome, ele passa como string
  76.             //do mesmo jeito
  77.             //*** ---> 1 quer dizer que estamos passando a primeira interrogação (primeira coluna)
  78.             $Create->bindValue(1, 'Chrome', PDO::PARAM_STR);
  79.            
  80.             $Create->bindValue(2, '122', PDO::PARAM_INT);
  81.            
  82.             $Create->execute();
  83.            
  84.             if($Create->rowCount()):
  85.                 echo "{$PDO->getConn()->lastInsertId()} - Cadastro com sucesso!<hr>";
  86.             endif;
  87.                                    
  88.         } catch (Exception $e) {
  89.             PhpErro($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
  90.         }
  91.        
  92.        
  93.         ?>
  94.     </body>
  95. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement