Advertisement
Guest User

Untitled

a guest
May 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. <?php
  2. class Database
  3. {
  4. /* Singleton ***********************************/
  5. private static $instance;
  6.  
  7. public static function init($name, $server, $user, $password)
  8. {
  9. self::$instance = new Database($name, $server, $user, $password);
  10. }
  11.  
  12. public static function get()
  13. {
  14. if(!self::$instance->is_open()) self::$instance->open();
  15. return self::$instance;
  16. }
  17.  
  18. /* Instance ***********************************/
  19. private $name, $server, $user, $password = '';
  20. private $connection = NULL;
  21.  
  22. public function Database($name, $server, $user, $password)
  23. {
  24. $this->name = $name;
  25. $this->server = $server;
  26. $this->user = $user;
  27. $this->password = $password;
  28. }
  29.  
  30. public function is_open()
  31. {
  32. return ($this->connection != NULL);
  33. }
  34.  
  35. public function open()
  36. {
  37. $this->connection = mysql_connect($this->server, $this->user, $this->password) or $this->dump_error('none');
  38. mysql_select_db($this->name, $this->connection) or die('Database "' . $this->name . '" does not exist! Contact system administrator.');
  39. }
  40.  
  41. public function close()
  42. {
  43. if($this->connection != NULL)
  44. {
  45. mysql_close($this->connection);
  46. $this->connection = NULL;
  47. }
  48. }
  49.  
  50. public function row($query)
  51. {
  52. $result = mysql_query($query, $this->connection) or $this->dump_error($query);
  53. if(mysql_num_rows($result) > 0)
  54. {
  55. return mysql_fetch_assoc($result);
  56. }
  57. else
  58. {
  59. return NULL;
  60. }
  61. }
  62.  
  63. public function rows($query)
  64. {
  65. $result = mysql_query($query, $this->connection) or $this->dump_error($query);
  66. $rows = array();
  67. if(mysql_num_rows($result) > 0)
  68. {
  69. while($row = mysql_fetch_assoc($result))
  70. {
  71. $rows[] = $row;
  72. }
  73. }
  74. return $rows;
  75. }
  76.  
  77. public function execute($query)
  78. {
  79. return mysql_query($query, $this->connection) or $this->dump_error($query);
  80. }
  81.  
  82. private function dump_error($query)
  83. {
  84. ?>
  85.  
  86. <div id="error">
  87. <h3>Database error!</h3>
  88. <p><strong>Query:</strong> <em><?=$query?></em></p>
  89. <p><strong>Message:</strong> <em><?=mysql_error()?></em></p>
  90. </div>
  91.  
  92. <?php
  93. }
  94. }
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement