Guest User

experimenta isto

a guest
Mar 14th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2. // myclass.php
  3. class MyClass{
  4.    
  5.     private $erro, $db, $resultado = null;
  6.    
  7.     public function __construct($dados = array()){
  8.         if(is_array($dados) && !empty($dados)){
  9.             try{
  10.                 $this->db = new PDO($dados["dsn"], $dados["usr"], $dados["psw"], $dados["opc"]);
  11.             } catch(PDOException $e){
  12.                 $this->erro = $e->getMessage();
  13.                 die("Ahm, algo nao esta bem...");
  14.             }
  15.         }
  16.     }
  17.    
  18.     public function selectAll($sql){
  19.         if($this->db){
  20.             $stmt = $this->db->prepare($sql);
  21.             if($stmt->execute()){
  22.                 $this->resultado = $stmt->fetchAll();
  23.                 return $this->resultado;
  24.             }
  25.         }
  26.         return;
  27.     }
  28.    
  29.     public function erro(){
  30.         return $this->erro;
  31.     }
  32. }
  33.  
  34. ?>
  35.  
  36. <?php
  37.  
  38. require_once 'myclass.php';
  39.  
  40. $myclass = new MyClass(["dsn"=>"mysql:host=localhost;dbname=nuwie;", "usr"=>"root", "psw"=>"", "opc"=>array()]);
  41.  
  42. $dados = $myclass->selectAll("SELECT * FROM tests");
  43.  
  44. foreach($dados as $dado){
  45.     print "{$dado["apelido"]}, {$dado["nome"]} <br/>";
  46. }
  47.  
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment