Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Db_DataBase
- {
- public $handle = null;
- private static $instance = NULL;
- protected $_bebug;
- private $count_query = 0;
- static public function instance()
- {
- if (self::$instance == NULL)
- {
- self::$instance = new self();
- }
- return self::$instance;
- }
- private function __construct() {}
- protected function connect()
- {
- if ($this->handle) return $this->handle;
- $config = Registry::get('db_config');
- $this->_bebug = $config['debug'];
- if ($this->_bebug == 2)
- self::DebugLog('[Start connect ' . date('d.m.Y H:i:s') . ' ]');
- $this->handle = mysql_pconnect($config['host'], $config['user'], $config['password']);
- $this->sql('SET NAMES utf8');
- mysql_select_db($config['db_name'], $this->handle);
- return $this->handle;
- }
- public function close()
- {
- if ($this->_bebug == 2)
- self::DebugLog('[end connect ' . date('d.m.Y H:i:s') . ' ]' . "\n\n");
- //mysql_close($this->handle);
- }
- public function __destruct()
- {
- if ($this->_bebug == 1) {
- echo "<br/><b>Всего {$this->count_query} запросов.</b><br/>";
- $this->count_query = 0;
- }
- // $this->close();
- }
- public function getLastId()
- {
- return mysql_insert_id($this->connect()) ? mysql_insert_id($this->connect()) : - 1;
- }
- public function quot( $value )
- {
- /*if (is_numeric($value) && !is_float($value)) {
- $value = (int)$value;
- } else {*/
- if (is_array($value))
- foreach (array_keys($value) as $k)
- $value[$k] = "'" . mysql_real_escape_string($value[$k], $this->connect()) . "'";
- else
- $value = "'" . mysql_real_escape_string($value, $this->connect()) . "'";
- /*}*/
- return $value;
- }
- public function getErrorNumber()
- {
- return mysql_errno($this->connect());
- }
- public function sql( $query )
- {
- if ($this->_bebug == 1) {
- $this->count_query++;
- echo "<b>{$this->count_query}:</b> {$query}<br />";
- }
- if ($this->_bebug == 2)
- self::DebugLog($query);
- return mysql_query($query, $this->connect());
- }
- public function sqlParse( $query )
- {
- $res = $this->sql($query);
- if ($res)
- {
- while($row = mysql_fetch_assoc($res))
- $qresult[] = $row;
- mysql_free_result($res);
- }
- return isset($qresult) ? $qresult : null;
- }
- public function insert( $table, $items )
- {
- $f_sql = $v_sql = array ();
- foreach ($items as $f => $v) {
- $f_sql[] = '`' . $f . '`';
- $v_sql[] = $v;
- }
- $query = 'INSERT INTO `' . $table . '` (' . implode(',', $f_sql) . ') VALUES (' . implode(',', $v_sql) . ')';
- $this->sql($query);
- return $this->getLastId();
- }
- public function replace( $table, $items )
- {
- $f_sql = $v_sql = array ();
- foreach ($items as $f => $v)
- {
- $f_sql[] = '`' . $f . '`';
- $v_sql[] = $v;
- }
- $query = 'REPLACE INTO `' . $table . '` (' . implode(',', $f_sql) . ') VALUES (' . implode(',', $v_sql) . ')';
- $this->sql($query);
- return $this->getLastId();
- }
- public function update( $table, $items, $where )
- {
- $sql = array ();
- foreach ($items as $f => $v)
- $sql[] = '`' . $f . '`=' . $v;
- $query = 'UPDATE `' . $table . '` SET ' . implode(',', $sql);
- if (! empty($where))
- $query .= ' WHERE ' . $where;
- return $this->sql($query);
- }
- public function delete( $table, $where )
- {
- $query = 'DELETE FROM `' . $table . '` WHERE ' . $where;
- return $this->sql($query);
- }
- private static function DebugLog( $str )
- {
- $file = ROOT_DIR . 'sql_tmp_' . date('d_m_Y') . '.txt';
- $handle = @fopen($file, 'a+');
- @fwrite($handle, $str . "\n");
- @fclose($handle);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement