Guest User

Untitled

a guest
Dec 19th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. SQLSTATE[HY000] [2002] No such file or directory
  2.  
  3. class Config {
  4. const dbhost = 'localhost';
  5. const dbname = 'dbbanco';
  6. const dbuser = 'dbsenha';
  7. const dbpass = 'dbusuario';
  8. }
  9.  
  10. class Connection extends Config {
  11. private $dbhost = parent::dbhost;
  12. private $dbname = parent::dbname;
  13. private $dbuser = parent::dbuser;
  14. private $dbpass = parent::dbpass;
  15. private $dbconn = null;
  16.  
  17. function __construct() {
  18. if (empty($this->dbconn)) {
  19. $dbparams = array(
  20. 'dbhost' => $this->dbhost,
  21. 'dbname' => $this->dbname,
  22. 'dbuser' => $this->dbuser,
  23. 'dbpass' => $this->dbpass
  24. );
  25. return $this->dbconn = $this->dbConnect($dbparams);
  26. }
  27. }
  28.  
  29. private function dbConnect(array $dbparams) {
  30. if (!empty($dbparams['dbhost']) && !empty($dbparams['dbname']) && !empty($dbparams['dbuser']) && !empty($dbparams['dbpass'])) {
  31. $dbopts = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  32. try {
  33. $this->dbconn = new PDO("mysql:dbhost=" . $dbparams['dbhost'] . ";dbname=" . $dbparams['dbname'] . "", $dbparams['dbuser'], $dbparams['dbpass'], $dbopts);
  34. return $this->dbconn;
  35. } catch (PDOException $error) {
  36. exit('<h1>Falha na conexão com o banco de dados.</h1><h2>Detalhes da falha:</h2><pre>' . $error->getMessage() . '</pre>');
  37. }
  38. }
  39. }
  40.  
  41. public function sqlQuery($query = null, $fetch = false, $fetchAll = false) {
  42. if (!empty($query)) {
  43. if ($fetch == true) {
  44. if ($fetchAll == true) {
  45. $this->dbconn = $this->dbconn->query($query);
  46. return $this->dbconn->fetchAll();
  47. } else {
  48. $this->dbconn = $this->dbconn->query($query);
  49. return $this->dbconn->fetch(PDO::FETCH_ASSOC);
  50. }
  51. } else {
  52. return $this->dbconn->query($query);
  53. }
  54. } else {
  55. return 'Nenhuma consulta SQL foi informada. Acesso ao banco de dados cancelado.';
  56. }
  57. }
  58. }
Add Comment
Please, Sign In to add comment