PHP MySQL Klasse

Januar 19, 2012 PHP & MySQL

Eine PHP Klasse die ich gerne Verwende :3

<?php

class Database {

    var $connection = '';
    var $prefix = '';
    var $queries;
    var $debug = false;
    var $logdir = '';
    var $mailto = 'ERROR@IHREDOMAIN.TLD';
    var $last_query;

    function __construct($info = array(
        'host' => 'localhost',
        'user' => 'MYSQLBENUTZER',
        'pass' => 'MYSQLPASSWORT',
        'database' => 'MYSQLDATENBANK',
        'prefix' => '',
        'port' => 3306,
        'debug' => true
    )) {
        $this->prefix = $info['prefix'];
        $this->debug = $info['debug'];
        $this->queries = 0;
        $this->logdir = dirname(__FILE__);
        $this->connect($info['host'], $info['user'], $info['pass'], $info['database'], $info['port']);

        $this->simpleQuery("SET NAMES 'utf8'");
    }

    function __destruct() {
        $this->closeDb($this->connection);
    }

    function connect($host, $user, $pass, $database, $port) {
        $this->connection = @mysql_connect($host . ':' . $port, $user, $pass);

        if (!$this->connection) {
            $this->displayError(mysql_error(), mysql_errno());
        } else {
            $this->select_db($database);
        }
    }

    function select_db($database) {
        $select = mysql_select_db($database, $this->connection);
        if (!$select) {
            $this->displayError(mysql_error(), mysql_errno());
        }
    }

    function closeDb($connection) {
        $close = @mysql_close($connection);
    }

    function select($select = array('select' => '*', 'from' => '', 'where' => '', 'limit' => '', 'group' => '', 'order' => '')) {
        $sql = 'SELECT ' . $select['select'] . ' FROM ' . $this->prefix . $select['from'];

        if (!empty($select['where'])) {
            $sql .= ' WHERE ' . $select['where'];
        }
        if (!empty($select['group'])) {
            $sql .= ' GROUP BY ' . $select['group'];
        }
        if (!empty($select['order'])) {
            $sql .= ' ORDER BY ' . $select['order'];
        }

        if (!empty($select['limit'])) {
            $sql .= ' LIMIT ' . $select['limit'];
        }

        return $this->simpleQuery($sql);
    }

    function insert($table, $insert = array()) {
        $sql = 'INSERT INTO ' . $this->prefix . $table;
        $array_key = array();
        $array_value = array();

        $i = 1;
        $a = '';
        $b = '';

        foreach ($insert as $key => $value) {
            $a .= ($i == 1) ? $key : ', ' . $key;
            $b .= ($i == 1) ? '"' . $value . '"' : ', "' . $value . '"';
            $i++;
        }

        $sql .= ' (' . $a . ') VALUES (' . $b . ')';
        return $this->simpleQuery($sql);
    }

    function lastID($connection = '') {
        $connection = (empty($connection)) ? $this->connection : $connection;
        return @mysql_insert_id($connection);
    }

    function update($table, $update = array(), $where = '', $limit = '') {
        $sql = 'UPDATE ' . $this->prefix . $table . ' SET ';

        $i = 1;
        $a = '';
        foreach ($update as $key => $value) {
            if (is_array($value)) {
                $a .= ($i == 1) ? $key . ' = \'' . $value[0] . '\'' . $value[1] : ', ' . $key . ' = \'' . $value[0] . '\'' . $value[1];
            } else {
                $a .= ($i == 1) ? $key . ' = \'' . $value . '\'' : ', ' . $key . ' = \'' . $value . '\'';
            }
            $i++;
        }

        $sql .= (!empty($where)) ? $a . ' WHERE ' . $where : $a;
        $sql .= (!empty($limit)) ? ' LIMIT ' . $limit : '';

        return $this->simpleQuery($sql);
    }

    function delete($table, $where = '', $limit = '') {
        $sql = 'DELETE FROM ' . $this->prefix . $table;
        $sql .= (!empty($where)) ? ' WHERE ' . $where : '';
        $sql .= (!empty($limit)) ? ' LIMIT ' . $limit : '';
        return $this->simpleQuery($sql);
    }

    function escape($string) {
        $escape = @mysql_real_escape_string($string, $this->connection);
        return $escape;
    }

    function fetch($query) {
        $res = mysql_fetch_object($query);
        return $res;
    }

    function getTotalRows($query) {
        $count = @mysql_num_rows($query);
        return $count;
    }

    function fetchArray($query) {
        $res = @mysql_fetch_array($query);
        return $res;
    }

    function fetchAssoc($query) {
        $res = @mysql_fetch_assoc($query);
        return $res;
    }

    function freeResult($ID = NULL) {
        $res = @mysql_free_result($query);
        return $res;
    }

    function simpleQuery($sql, $link = '') {
        $start = microtime(true);
        if (empty($link))
            $link = $this->connection;
        $this->last_query = $sql;
        $query = @mysql_query($sql, $link);
        $time = microtime(true) - $start;
        if ($time > 1.5) {
            $this->write_slow_log($time, $sql);
        }
        if (!$query) {
            $this->displayError(mysql_error(), mysql_errno());
        } else {
            $this->queries++;
            return $query;
        }
    }

    function GetQueries() {
        return $this->queries;
    }

    function displayError($error, $errno) {
        echo '<h2>Database Error</h2>';
        echo '<p>Sorry there was an error, with the database.<br />Please try again later.</p>';

        $this->write_log($error);
        if(FALSE) {
            $this->send_error($error);
        }

        if ($this->debug == true) {
            echo '<div style="background-color:#EEE;color:#222;padding:3px;font-size:12px;;font-family:Serif, sans-serif;">';

            echo '<p><b>MySQL Errno:</b> ' . $errno . '</p>';

            echo '<p><b>MySQL Error:</b> ' . $error . '</p>';

            if (!empty($this->last_query)) {
                echo '<p><b>MySQL Query:</b> ' . $this->last_query . '</p>';
            }

            echo '<pre style="padding: 5px; font-size: 10px; font-family: Verdana">';
            foreach (debug_backtrace() as $EI => $ErrArray) {
                echo 'Zeile: <u>' . $ErrArray['line'] . "</u>\n";
                echo 'File: <u>' . $ErrArray['file'] . "</u>\n";

                if (isset($ErrArray['function'])) {
                    echo 'Function: ' . $ErrArray['function'] . "\n";
                }

                if (isset($ErrArray['object'])) {
                    echo 'Object: ' . print_r($ErrArray['object'], true) . "\n";
                }
            }
            echo '<br /><br /></pre>';

            echo '</div>';
        }

        die();
    }

    function optimize() {
        $sql = $this->simpleQuery('SHOW TABLES');
        while ($row = $this->fetchAssoc($sql)) {
            foreach ($row as $db => $tablename) {
                $this->simpleQuery('OPTIMIZE TABLE `' . $tablename . '`');
            }
        }
    }

    function send_error($text) {
            $mysql_error = date('d.m.Y-H:i:s') . ' ' . $_SERVER['REMOTE_ADDR'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $text;
            if(mail($this->mailto, "MySQL-Error", $mysql_error)) {
                return TRUE;
            } else {
                return FALSE;
            }
    }

    function write_log($text) {
        if (file_exists($this->logdir . '/logs/mysql_error.log') && is_writable($this->logdir . '/logs/mysql_error.log')) {
            $file = @fopen($this->logdir . '/logs/mysql_error.log', 'a');
            if ($file) {
                fwrite($file, date('d.m.Y-H:i:s') . ' ' . $_SERVER['REMOTE_ADDR'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $text . "\r\n");
            }
            fclose($file);
        }
    }

    function write_slow_log($time, $text) {
        if (file_exists($this->logdir . '/logs/mysql_slow.log') && is_writable($this->logdir . '/logs/mysql_slow.log')) {
            $file = @fopen($this->logdir . '/logs/mysql_slow.log', 'a');
            if ($file) {
                fwrite($file, date('d.m.Y-H:i:s') . ' ' . $time . ' ' . $_SERVER['REMOTE_ADDR'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $text . "\r\n");
            }
            fclose($file);
        }
    }

}

?>

Write a Comment

You must be logged in to post a comment.