Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // myclass.php
- class MyClass{
- private $erro, $db, $resultado = null;
- public function __construct($dados = array()){
- if(is_array($dados) && !empty($dados)){
- try{
- $this->db = new PDO($dados["dsn"], $dados["usr"], $dados["psw"], $dados["opc"]);
- } catch(PDOException $e){
- $this->erro = $e->getMessage();
- die("Ahm, algo nao esta bem...");
- }
- }
- }
- public function selectAll($sql){
- if($this->db){
- $stmt = $this->db->prepare($sql);
- if($stmt->execute()){
- $this->resultado = $stmt->fetchAll();
- return $this->resultado;
- }
- }
- return;
- }
- public function erro(){
- return $this->erro;
- }
- }
- ?>
- <?php
- require_once 'myclass.php';
- $myclass = new MyClass(["dsn"=>"mysql:host=localhost;dbname=nuwie;", "usr"=>"root", "psw"=>"", "opc"=>array()]);
- $dados = $myclass->selectAll("SELECT * FROM tests");
- foreach($dados as $dado){
- print "{$dado["apelido"]}, {$dado["nome"]} <br/>";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment