Advertisement
Guest User

Untitled

a guest
May 7th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. <?php
  2.  
  3. // classe conecta
  4.  
  5. class conecta {
  6.  
  7. private $hostname, $username, $password, $name_db, $mysqli, $nome, $sobrenome;
  8.  
  9. public function __construct() {
  10.  
  11.     $this->hostname = "localhost";
  12.     $this->username = "root";
  13.     $this->password = "abzj6220";
  14.     $this->name_db = "alternativa";
  15.  
  16. }
  17.  
  18. private function conectar() {
  19.  
  20.     $this->mysqli = new mysqli($this->hostname, $this->username, $this->password, $this->name_db);
  21.  
  22.     if ( mysqli_connect_errno() ) {
  23.  
  24.         throw new Exception('Problema ao conectar o banco de dados');
  25.  
  26.     }
  27.  
  28. }
  29.  
  30. public function inserir($nome, $sobrenome) {
  31.  
  32.     $this->nome = $nome;
  33.     $this->sobrenome = $sobrenome;
  34.  
  35.     if ( !isset($this->mysqli) ) {
  36.  
  37.         $this->conectar();
  38.  
  39.     }
  40.  
  41.     if ( $stmt = $this->mysqli->prepare("INSERT INTO tabela (nome,sobrenome) VALUES (?,?)") ) {
  42.  
  43.         $stmt->bind_param("ii", $this->nome, $this->sobrenome);
  44.         $stmt->execute();
  45.         $stmt->close();
  46.  
  47.         return true;
  48.  
  49.     } else {
  50.  
  51.         throw new herdaException('Problema ao inserir dados');
  52.  
  53.       }
  54.  
  55. }
  56.  
  57. }
  58.  
  59. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement