Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. class Db
  2. {
  3. private $host = 'localhost';
  4. private $database = 'test';
  5. private $name = 'root';
  6. private $pass = 'root';
  7. public $dsn;
  8. private $bConnected = false;
  9. public $result;
  10. public $pdo;
  11. public $query;
  12. public $param;
  13. private $charset = 'utf8';
  14.  
  15. public function __construct($host, $name, $pass, $db)
  16. {
  17. $this->host = $host;
  18. $this->name = $name;
  19. $this->pass = $pass;
  20. $this->database = $db;
  21.  
  22. $this->connect();
  23. }
  24.  
  25. public function connect()
  26. {
  27. global $settings;
  28. try {
  29. $this->dsn = "mysql:host=$this->host;dbname=$this->database;charset=$this->charset";
  30. $opt = [
  31. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  32. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  33. PDO::ATTR_EMULATE_PREPARES => false,
  34. ];
  35. $this->pdo = new PDO($this->dsn, $this->name, $this->pass, $opt);
  36.  
  37. # Connection succeeded, set the boolean to true.
  38. $this->bConnected = true;
  39. } catch (PDOException $e) {
  40. #Get the message with error
  41. die($e->getMessage());
  42. }
  43. }
  44.  
  45. public function closeConnection()
  46. {
  47. # Set the PDO object to null to close the connection
  48. $this->pdo = null;
  49. }
  50.  
  51. public function getOne($query, $variable)
  52. {
  53. var_dump($this->pdo);
  54. if (!$this->bConnected) {
  55. $this->connect();
  56. }
  57.  
  58. $row = array();
  59. $pdo = $this->pdo;
  60.  
  61. try {
  62. if (is_string($query)) {
  63. $pdo->prepare($query);
  64. $this->result = $pdo->execute($variable);
  65. } else {
  66. $this->result = $query;
  67. }
  68. if (!empty($this->result)) {
  69. $row = $this->result;
  70. }
  71. } catch (PDOException $e) {
  72. $e->getMessage();
  73. die();
  74. }
  75. return $row;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement