Guest User

Untitled

a guest
Apr 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. <?php
  2. class Database {
  3. var $connected = 0;
  4. var $db_type = DATABASE_TYPE;
  5. var $db_hostname = DATABASE_HOST;
  6. var $db_username = DATABASE_USER;
  7. var $db_password = DATABASE_PASS;
  8. var $db_database = DATABASE_DATA;
  9. function __construct($type = null, $hostname = null, $username = null, $password = null) {
  10. if (($type != null) && ($hostname != null) && ($username != null) && ($password != null)) {
  11. $this->db_type = $type; // Add initial support for diff kinds of db types
  12. $this->db_hostname = $hostname;
  13. $this->db_username = $username;
  14. $this->db_password = $password;
  15. }
  16. }
  17. public function Test() {
  18. if ($this->Connect () == true) {
  19. unset ( $this->pdo );
  20. return true;
  21. }
  22. }
  23. public function Select_DB($db) {
  24. $this->db_database = $db;
  25. return $this->db_database;
  26. }
  27. public function Connect() {
  28. // Check for database type, for now we start with mysql support only, this can be extended
  29. if ($this->db_type == strtolower ( "mysql" )) {
  30. try {
  31. $this->pdo = new PDO ( "mysql:host=$this->db_hostname;dbname=$this->db_database", $this->db_username, $this->db_password );
  32. } catch ( PDOException $Exception ) {
  33. $this->connected = 0;
  34. throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
  35. }
  36. $this->connected = 1;
  37. return true; // We are connected and database is selected, return true
  38. }
  39. }
  40. public function Bind() {
  41. $this->bind = func_get_args ();
  42. }
  43. public function Fetch($what, $from, $where = null, $i = 1) {
  44. if ($where == null)
  45. $query = 'SELECT ' . $what . ' FROM ' . $from . '';
  46. else
  47. $query = 'SELECT ' . $what . ' FROM ' . $from . ' ' . $where . '';
  48. try {
  49. if ($this->connected == 0)
  50. $this->Connect ();
  51. $this->stmt = $this->pdo->prepare ( $query );
  52. if (isset ( $this->bind ))
  53. foreach ( $this->bind as $key => $arg )
  54. $this->stmt->bindValue ( $i ++, $arg );
  55. $this->stmt->execute ();
  56. } catch ( PDOException $Exception ) {
  57. throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
  58. }
  59. return $this->stmt->fetchALL ( PDO::FETCH_OBJ );
  60. }
  61. public function Update($table, $set, $where = null, $i = 1) {
  62. if ($where == null)
  63. $query = "UPDATE $table SET $set";
  64. else
  65. $query = "UPDATE $table SET $set $where";
  66. try {
  67. if ($this->connected == 0)
  68. $this->Connect ();
  69. $this->stmt = $this->pdo->prepare ( $query );
  70. if (isset ( $this->bind ))
  71. foreach ( $this->bind as $key => $arg )
  72. $this->stmt->bindValue ( $i ++, $arg );
  73. $this->stmt->execute ();
  74. } catch ( PDOException $Exception ) {
  75. throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
  76. }
  77. return $this->stmt->rowCount ();
  78. }
  79. public function Insert($table, $name, $data, $i = 1) {
  80. $query = "INSERT INTO $table ( $name ) VALUES ( $data )";
  81. try {
  82. if ($this->connected == 0)
  83. $this->Connect ();
  84. $this->stmt = $this->pdo->prepare ( $query );
  85. if (isset ( $this->bind ))
  86. foreach ( $this->bind as $key => $arg ) {
  87. $this->stmt->bindValue ( $i ++, $arg );
  88. }
  89. $this->stmt->execute ();
  90. } catch ( PDOException $Exception ) {
  91. throw new PDOException ( $Exception->getMessage (), ( int ) $Exception->getCode () );
  92. }
  93. return $this->stmt->rowCount ();
  94. }
  95. }
  96. ?>
Add Comment
Please, Sign In to add comment