Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database
- {
- /* Singleton ***********************************/
- private static $instance;
- public static function init($name, $server, $user, $password)
- {
- self::$instance = new Database($name, $server, $user, $password);
- }
- public static function get()
- {
- if(!self::$instance->is_open()) self::$instance->open();
- return self::$instance;
- }
- /* Instance ***********************************/
- private $name, $server, $user, $password = '';
- private $connection = NULL;
- public function Database($name, $server, $user, $password)
- {
- $this->name = $name;
- $this->server = $server;
- $this->user = $user;
- $this->password = $password;
- }
- public function is_open()
- {
- return ($this->connection != NULL);
- }
- public function open()
- {
- $this->connection = mysql_connect($this->server, $this->user, $this->password) or $this->dump_error('none');
- mysql_select_db($this->name, $this->connection) or die('Database "' . $this->name . '" does not exist! Contact system administrator.');
- }
- public function close()
- {
- if($this->connection != NULL)
- {
- mysql_close($this->connection);
- $this->connection = NULL;
- }
- }
- public function row($query)
- {
- $result = mysql_query($query, $this->connection) or $this->dump_error($query);
- if(mysql_num_rows($result) > 0)
- {
- return mysql_fetch_assoc($result);
- }
- else
- {
- return NULL;
- }
- }
- public function rows($query)
- {
- $result = mysql_query($query, $this->connection) or $this->dump_error($query);
- $rows = array();
- if(mysql_num_rows($result) > 0)
- {
- while($row = mysql_fetch_assoc($result))
- {
- $rows[] = $row;
- }
- }
- return $rows;
- }
- public function execute($query)
- {
- return mysql_query($query, $this->connection) or $this->dump_error($query);
- }
- public function last_inserted_id()
- {
- return mysql_insert_id($this->connection);
- }
- private function dump_error($query)
- {
- ?>
- <div id="error">
- <h3>Database error!</h3>
- <p><strong>Query:</strong> <em><?=$query?></em></p>
- <p><strong>Message:</strong> <em><?=mysql_error()?></em></p>
- </div>
- <?php
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement