Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Db
- {
- private $host = 'localhost';
- private $database = 'test';
- private $name = 'root';
- private $pass = 'root';
- public $dsn;
- private $bConnected = false;
- public $result;
- public $pdo;
- public $query;
- public $param;
- private $charset = 'utf8';
- public function __construct($host, $name, $pass, $db)
- {
- $this->host = $host;
- $this->name = $name;
- $this->pass = $pass;
- $this->database = $db;
- $this->connect();
- }
- public function connect()
- {
- global $settings;
- try {
- $this->dsn = "mysql:host=$this->host;dbname=$this->database;charset=$this->charset";
- $opt = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- ];
- $this->pdo = new PDO($this->dsn, $this->name, $this->pass, $opt);
- # Connection succeeded, set the boolean to true.
- $this->bConnected = true;
- } catch (PDOException $e) {
- #Get the message with error
- die($e->getMessage());
- }
- }
- public function closeConnection()
- {
- # Set the PDO object to null to close the connection
- $this->pdo = null;
- }
- public function getOne($query, $variable)
- {
- var_dump($this->pdo);
- if (!$this->bConnected) {
- $this->connect();
- }
- $row = array();
- $pdo = $this->pdo;
- try {
- if (is_string($query)) {
- $pdo->prepare($query);
- $this->result = $pdo->execute($variable);
- } else {
- $this->result = $query;
- }
- if (!empty($this->result)) {
- $row = $this->result;
- }
- } catch (PDOException $e) {
- $e->getMessage();
- die();
- }
- return $row;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement