Advertisement
Guest User

banco.php

a guest
Mar 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. <?php
  2. //banco.php
  3.  
  4. class Banco{
  5.  
  6. private $pdo;
  7. private $numRows;
  8. private $array;
  9.  
  10.  
  11. public function __construct($host, $dbname, $dbuser, $dbpass){
  12.  
  13. try {
  14. $this->pdo = new PDO("mysql:dbname=".$dbname.";host=".$host, $dbuser, $dbpass);
  15. } catch (PDOException $e) {
  16. echo "Falha ao conectar = ". $e->getMessage();
  17. }
  18. }
  19.  
  20. public function query($sql){
  21. $query = $this->pdo->query($sql);
  22. $this->numRows = $query->rowCount();
  23. $this->array = $query->fetchAll();
  24. }
  25.  
  26. public function numRows(){
  27. return $this->numRows;
  28. }
  29.  
  30. public function result(){
  31. return $this->array;
  32. }
  33.  
  34. public function insert($table, $data) {
  35. if(!empty($table) && (is_array($data) && count($data) > 0)):
  36. $sql = "INSERT INTO ".$table." SET ";
  37. $dados = array();
  38. foreach ($data as $chave => $valor):
  39. $dados[] = $chave." = '".addslashes($valor)."'";
  40. endforeach;
  41. $sql .= implode(", ", $dados);
  42. var_dump($dados);
  43. endif;
  44. $this->pdo->query($sql);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement