fork download
  1. <?php
  2. class ConnectDB {
  3. public $pdo;
  4. private $driver, $host, $port, $base, $user, $pass;
  5.  
  6. public function __construct() {
  7. $this->setDriver('mysql');
  8. $this->setHost('localhost');
  9. $this->setPort('3306');
  10. $this->setBase('bsn');
  11. $this->setUser('root');
  12. $this->setPass('');
  13. $this->connect();
  14. }
  15.  
  16. public function connect() {
  17. try {
  18. $this->pdo = new PDO("{$this->getDriver()}:host={$this->getHost()};port={$this->getPort()};dbname={$this->getBase()}", $this->getUser(), $this->getPass());
  19. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  20. $this->pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
  21. } catch (PDOException $e) {
  22. die('Ocorreu um erro na conexão com o banco de dados.');
  23. }
  24. }
  25.  
  26. public function setDriver($str) {
  27. $this->driver = $str;
  28. }
  29.  
  30. public function setHost($str) {
  31. $this->host = $str;
  32. }
  33.  
  34. public function setPort($str) {
  35. $this->port = $str;
  36. }
  37.  
  38. public function setBase($str) {
  39. $this->base = $str;
  40. }
  41.  
  42. public function setUser($str) {
  43. $this->user = $str;
  44. }
  45.  
  46. public function setPass($str) {
  47. $this->pass = $str;
  48. }
  49.  
  50. public function getPDO() {
  51. return $this->pdo;
  52. }
  53.  
  54. public function getDriver() {
  55. return $this->driver;
  56. }
  57.  
  58. public function getHost() {
  59. return $this->host;
  60. }
  61.  
  62. public function getPort() {
  63. return $this->port;
  64. }
  65.  
  66. public function getBase() {
  67. return $this->base;
  68. }
  69.  
  70. public function getUser() {
  71. return $this->user;
  72. }
  73.  
  74. public function getPass() {
  75. return $this->pass;
  76. }
  77. }
  78.  
  79. class ManipulateData extends ConnectDB {
  80. public function select($query) {
  81. $stmt = $this->getPDO()->prepare($query);
  82. $stmt->execute();
  83. return $stmt->fetchAll();
  84. }
  85. }
  86. $obj = new ManipulateData();
  87. print_r(obj.select('SELECT * FROM vagas'));
  88.  
  89. //https://pt.stackoverflow.com/q/331916/101
Success #stdin #stdout 0.01s 82560KB
stdin
Standard input is empty
stdout
Ocorreu um erro na conexão com o banco de dados.