MySQL Class for PHP

11 Months ago(Updated in 11 Months ago) PHP 0

<?php
//VTwo Group - https://vtwo.org
class MySQL {
    var $error = '';
    var $MySQLerror = '';
    var $host = '';
    var $username = '';
    var $password = '';
    var $dbname = '';
    var $conn = 0;
    var $rslt = false;
 
    function __construct($dbname = '') {
        if ($dbname) {
            $this->dbname = $dbname;
        }
    }
 
    public function MySQLerror() {
        return $this->MySQLerror;
    }
 
    public function error() {
        switch ($this->error) {
            case 'db-error':
                return 'DB error.';
            case 'connect-error':
                return 'Internal error.';
            default:
                return $this->error;
        }
    }
 
    public function transaction() {
        $this->query('START TRANSACTION');
    }
 
    public function rollback() {
        $this->query('ROLLBACK');
    }
 
    public function commit() {
        $this->query('COMMIT');
    }
 
    public function connect($dbname = '') {
        if (!$dbname) {
            $dbname = $this->dbname;
        }
 
        $this->conn = @mysql_connect($this->host, $this->username, $this->password);
        $this->MySQLerror = mysql_error();
        if (!$this->conn) {
            $this->error = 'db-error';
        }
 
        $this->select_db($dbname);
    }
 
    public function select_db($dbname) {
        $this->error = '';
 
        $rslt = mysql_select_db($dbname, $this->conn);
        $this->MySQLerror = mysql_error();
        if (!$rslt) {
            $this->error = 'connect-error';
        }
    }
 
    public function escape_string($str) {
        if (get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
 
        if (function_exists("mysql_real_escape_string") and $this->conn) //check if this function exists
            {
            $str = mysql_real_escape_string($str, $this->conn);
            $this->MySQLerror = mysql_error();
        }
        else {
            $str = addslashes($str); //for PHP version < 4.3.0 use addslashes
        }
 
        return $str;
    }
 
    public function insert($table, $inserts, $ignore = false) {
        if ($ignore) {
            $ignore = ' IGNORE';
        }
        {
            $ignore = '';
        }
 
        $query = "INSERT$ignore INTO `" . $table . "` ";
        $comma = $keys = $values = '';
        foreach ($inserts as $k => $v) {
            $keys .= "$comma`$k`";
            $values .= "$comma'" . $this->escape_string($v) . "'";
 
            if (!$comma)
                $comma = ',';
        }
 
        $query .= "($keys) VALUES ($values)";
        $this->query($query);
    }
 
    public function update($table, $fields, $where = '') {
        $query = "UPDATE `" . $table . "` SET ";
        $comma = '';
        foreach ($fields as $k => $v) {
            $query .= "$comma`$k`" . " = '" . $this->escape_string($v) . "'";
 
            if (!$comma)
                $comma = ',';
        }
 
        $query .= " WHERE " . $where;
        $this->query($query);
    }
 
    public function delete($table, $where = '') {
        if ($where) {
            $this->query("DELETE FROM `$table` WHERE $where");
        } elseif ($where == 'TABLE') {
            $this->query("DELETE FROM `$table`");
        }
    }
 
    public function query($query) {
        $this->error = '';
 
        $rslt = mysql_query($query, $this->conn);
        $this->MySQLerror = mysql_error();
        if (!$rslt) {
            $this->error = 'db-error';
        }
 
        $this->rslt = $rslt;
        return $rslt;
    }
 
    public function numrows() {
        if ($this->rslt) {
            return mysql_num_rows($this->rslt);
        }
        else {
            return false;
        }
    }
 
    public function fetch() {
        if ($this->rslt) {
            return mysql_fetch_array($this->rslt);
        }
        else {
            return false;
        }
    }
 
    public function free_result() {
        if ($this->rslt) {
            return mysql_free_result($this->rslt);
        }
        else {
            return false;
        }
    }
 
    public function data_seek($row_number = 0) {
        if ($this->rslt) {
            return mysql_data_seek($this->rslt, $row_number);
        }
        else {
            return false;
        }
    }
 
    public function result($row, $field) {
        if ($this->rslt) {
            return mysql_result($this->rslt, $row, $field);
        }
        else {
            return false;
        }
    }
 
    public function lastInsertID() {
        if ($this->rslt) {
            $this->query('SELECT LAST_INSERT_ID()');
            return $this->result(0, 0);
        }
        else {
            return false;
        }
    }
}
?>

Tags

PHP MySQL

Comments

No comment here.
captcha image reload