
		henbran
		
		(usa Debian)
		
		Enviado em 28/12/2010 - 22:51h 
		<?php
class Banco {
    public $con;
    public $msg;
    public $dC;
    private $tipo = "mysql";
    private $host = "localhost";
    private $bd = "nomeDoBancoDeDados";
    private $user = "usuarioDoBancoDeDados";
    private $pass = "senhaDoBancoDeDados";
//========================================================================================
    public function conectar() {
        $this->tipoBd();
        if ($this->con) {
            try {
                $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                $this->msg = "Erro de conexão com o banco de dados: Código: " . $e->getCode() . "Mensagem " . $e->getMessage() . "hora: " . date('H:i:s');
            }
        } else {
            echo utf8_encode("Erro na definição PDO do banco de dados!");
            exit;
        }
    }
//========================================================================================
    private function tipoBd() {       
                
        try {
            switch ($this->tipo) {
                case "mysql":
                    try {
                        $this->con = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->bd, $this->user, $this->pass);
                    } catch (Exception $e) {
                        $this->msg = "Erro de conexão com o banco de dados MySql. " . $e->getMessage();
                    }
                    break;
                case "pgsql":
                    $this->con = new PDO("pgsql:dbname={" . $this->bd . "};user={" . $this->user . "}; password={" . $this->pass . "};host=" . $this->host);
                    break;
                case "oci8":
                    $tns = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" . $this->host . ")(PORT=1521)))(CONNECT_DATA=(SID=" . $this->bd . ")))";
                    $this->con = new PDO("oci:dbname=" . $tns, $this->user, $this->pass, array(PDO::ATTR_PERSISTENT => true));
                    break;
                case "mssql":
                    $this->con = new PDO("mssql:host={" . $this->host . "},1433;dbname={" . $this->bd . "}", $this->user, $this->pass);
                    break;
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }
}
?>
========================================================
========================================================
Onde você for usar esta classe, no inicio do arquivo, chame-a:
...
require_once("Banco.php");
...
Depois instancie a classe:
...
$banco = new Banco();
$banco->conectar();
$sql = "SELECT * FROM TABELA";
$rs = $this->Banco->con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$rs->execute(); //executa a instrução sql
$retorno[n_reg] = $rs->rowCount();
$retorno[n_cols] = $rs->columnCount();
$retorno[res] = $rs->fetchAll(PDO::FETCH_ASSOC); //Retorna todas as linhas (registros) como um array
========================================================
Precisando ... estamos a disposição ...
Boa sorte