Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SQLSTATE[HY000] [2002] No such file or directory
- class Config {
- const dbhost = 'localhost';
- const dbname = 'dbbanco';
- const dbuser = 'dbsenha';
- const dbpass = 'dbusuario';
- }
- class Connection extends Config {
- private $dbhost = parent::dbhost;
- private $dbname = parent::dbname;
- private $dbuser = parent::dbuser;
- private $dbpass = parent::dbpass;
- private $dbconn = null;
- function __construct() {
- if (empty($this->dbconn)) {
- $dbparams = array(
- 'dbhost' => $this->dbhost,
- 'dbname' => $this->dbname,
- 'dbuser' => $this->dbuser,
- 'dbpass' => $this->dbpass
- );
- return $this->dbconn = $this->dbConnect($dbparams);
- }
- }
- private function dbConnect(array $dbparams) {
- if (!empty($dbparams['dbhost']) && !empty($dbparams['dbname']) && !empty($dbparams['dbuser']) && !empty($dbparams['dbpass'])) {
- $dbopts = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- try {
- $this->dbconn = new PDO("mysql:dbhost=" . $dbparams['dbhost'] . ";dbname=" . $dbparams['dbname'] . "", $dbparams['dbuser'], $dbparams['dbpass'], $dbopts);
- return $this->dbconn;
- } catch (PDOException $error) {
- exit('<h1>Falha na conexão com o banco de dados.</h1><h2>Detalhes da falha:</h2><pre>' . $error->getMessage() . '</pre>');
- }
- }
- }
- public function sqlQuery($query = null, $fetch = false, $fetchAll = false) {
- if (!empty($query)) {
- if ($fetch == true) {
- if ($fetchAll == true) {
- $this->dbconn = $this->dbconn->query($query);
- return $this->dbconn->fetchAll();
- } else {
- $this->dbconn = $this->dbconn->query($query);
- return $this->dbconn->fetch(PDO::FETCH_ASSOC);
- }
- } else {
- return $this->dbconn->query($query);
- }
- } else {
- return 'Nenhuma consulta SQL foi informada. Acesso ao banco de dados cancelado.';
- }
- }
- }
Add Comment
Please, Sign In to add comment