Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. $dados = Connection::select("SELECT * FROM tabela");
  2.  
  3. $rows = $dados->fetchAll();
  4. $count = count($rows);
  5.  
  6. <?php
  7.  
  8. class Connection {
  9.  
  10. public static $conn;
  11.  
  12. public static function open($name) {
  13. if (file_exists("config/{$name}.ini")) {
  14. $db = parse_ini_file("config/{$name}.ini");
  15. } else {
  16. throw new exception("Arquivo '$name' nao encontrado");
  17. }
  18.  
  19. $user = isset($db['user']) ? $db['user'] : NULL;
  20. $pass = isset($db['pass']) ? $db['pass'] : NULL;
  21. $name = isset($db['name']) ? $db['name'] : NULL;
  22. $host = isset($db['host']) ? $db['host'] : NULL;
  23. $type = isset($db['type']) ? $db['type'] : NULL;
  24. $port = isset($db['port']) ? $db['port'] : NULL;
  25.  
  26.  
  27. switch ($type) {
  28. case 'pgsql':
  29. $port = $port ? $port : '5432';
  30. $conn = new PDO("pgsql:dbname={$name}; user={$user}; password={$pass};host=$host;port={$port};");
  31. break;
  32. case 'mysql':
  33. $port = $port ? $port : '3306';
  34. $conn = new PDO("mysql:host={$host};port={$port};dbname={$name}", $user, $pass);
  35. break;
  36. case 'sqlite':
  37. $conn = new PDO("sqlite:{$name}");
  38. break;
  39. case 'ibase':
  40. $conn = new PDO("firebird:dbname={$name}", $user, $pass);
  41. break;
  42. case 'oci8':
  43. $conn = new PDO("oci:dbname={$name}", $user, $pass);
  44. break;
  45. case 'mssql':
  46. $conn = new PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass);
  47. break;
  48. }
  49.  
  50. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  51.  
  52. self::$conn = $conn;
  53. }
  54.  
  55. public static function select($sql) {
  56. return self::$conn->query($sql);
  57. }
  58.  
  59. public static function exec($sql, $ret_id = false) {
  60. $retorno = self::$conn->exec($sql);
  61. if ($ret_id)
  62. $retorno = self::$conn->lastInsertId();
  63. return $retorno;
  64. }
  65.  
  66. public static function close() {
  67. self::$conn = null;
  68. }
  69.  
  70. }
  71.  
  72. host = localhost
  73. name = sistema
  74. user = root
  75. pass = root
  76. type = mysql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement