daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Jan 20th, 2017 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.     class Connection {
  4.  
  5.         public static $conn;
  6.  
  7.         //lê o arquivo .ini com as config
  8.  
  9.         public static function open($name) {
  10.             if (file_exists("config/{$name}.ini")) {
  11.                 $db = parse_ini_file("config/{$name}.ini");
  12.             } else {
  13.                 throw new exception("Arquivo '$name' nao encontrado");
  14.             }
  15.  
  16.             $user = isset($db['user']) ? $db['user'] : NULL;
  17.             $pass = isset($db['pass']) ? $db['pass'] : NULL;
  18.             $name = isset($db['name']) ? $db['name'] : NULL;
  19.             $host = isset($db['host']) ? $db['host'] : NULL;
  20.             $type = isset($db['type']) ? $db['type'] : NULL;
  21.             $port = isset($db['port']) ? $db['port'] : NULL;
  22.  
  23.  
  24.             switch ($type) {
  25.                 case 'pgsql':
  26.                     $port = $port ? $port : '5432';
  27.                     $conn = new PDO("pgsql:dbname={$name}; user={$user}; password={$pass};host=$host;port={$port};");
  28.                     break;
  29.                 case 'mysql':
  30.                     $port = $port ? $port : '3306';
  31.                     $conn = new PDO("mysql:host={$host};port={$port};dbname={$name}", $user, $pass);
  32.                     break;
  33.                 case 'sqlite':
  34.                     $conn = new PDO("sqlite:{$name}");
  35.                     break;
  36.                 case 'ibase':
  37.                     $conn = new PDO("firebird:dbname={$name}", $user, $pass);
  38.                     break;
  39.                 case 'oci8':
  40.                     $conn = new PDO("oci:dbname={$name}", $user, $pass);
  41.                     break;
  42.                 case 'mssql':
  43.                     $conn = new PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
  44.                     break;
  45.             }
  46.  
  47.             $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  48.  
  49.             self::$conn = $conn;
  50.         }
  51.  
  52.         public static function select($sql) {
  53.             return self::$conn->query($sql);
  54.         }
  55.  
  56.         public static function exec($sql, $ret_id = false) {
  57.             $retorno = self::$conn->exec($sql);
  58.             if ($ret_id)
  59.                 $retorno = self::$conn->lastInsertId();
  60.             return $retorno;
  61.         }
  62.  
  63.         public static function close() {
  64.             self::$conn = null;
  65.         }
  66.  
  67.     }
  68.    
  69. host =
  70. name = localhost/dados/banco.fdb
  71. user = SYSDBA
  72. pass = 1234
  73. type = ibase
  74.    
  75. Connection::open('ibase');
  76.  
  77. $sql = Connection::select('SELECT * FROM banco');//Select
  78.  
  79. $sql = Connection::exec('INSERT INTO banco (campo) VALUES ('Teste')');//Insert
  80.  
  81. Connection::close('ibase');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top