Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. <?php
  2.  
  3. class Conexao {
  4. private $host = null;
  5. private $banco = null;
  6. private $usuario = null;
  7. private $senha = null;
  8. public $conn = null;
  9. public $sql = null;
  10. public $result = null;
  11.  
  12. public function __construct($host, $banco, $usuario, $senha) {
  13. $this->host = $host;
  14. $this->banco = $banco;
  15. $this->usuario = $usuario;
  16. $this->senha = $senha;
  17. }
  18.  
  19. public function __get($atributo) {
  20. return $this->atributo;
  21. }
  22.  
  23. public function __set($atributo, $valor) {
  24. $this->$atributo = $valor;
  25. }
  26.  
  27. public function conexaoComBanco() {
  28.  
  29. try {
  30. $this->conn = new PDO("mysql:host=$this->host; dbname=$this->banco, '$this->usuario', '$this->senha'");
  31. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  32. echo 'Conexão realizada com sucesso!';
  33.  
  34. } catch(PDOException $e) {
  35. echo $e->getMessage();
  36. }
  37. }
  38. }
  39.  
  40. $conexao = new Conexao('localhost', 'bancoteste', 'wilder', '12345');
  41.  
  42. $conexao->conexaoComBanco();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement