[MySQL] Easy xMySQL class for beginners

Results 1 to 4 of 4
  1. #1
    Member MaestroS is offline
    MemberRank
    Aug 2006 Join Date
    PolandLocation
    83Posts

    [MySQL] Easy xMySQL class for beginners

    xMySQL class is a class wrote in PHP language.
    It is for basic MySQL database managing.

    It includes functions wich are for example:
    - connecting,
    - closing connection,
    - querying,
    - fetching,
    - returning amount of rows,
    - returning affected rows,
    - creating database,
    - deleting database,
    - clearing memory,

    Functions:
    - xConnect(); - this function's establishing connection
    - xShutdown(CONNECTION); - this function's closing connection given in QUERY argument
    - xQuery(QUERY); - this function's executing provided query
    - xRows(QUERY); - this function's returning amount of rows from provided query
    - [NEW] xSQL(FILE); - this file imports *.sql file and executes all its contents
    - xFetch(QUERY, MODE); - this function's returning an array of provided MODE from provided QUERY.
    -- MODES FOR xFetch
    : _FA - using [mysql_fetch_assoc] function for query
    : _FR - using [mysql_fetch_row] function for query
    : _FY - using [mysql_fetch_array] function for query
    : default's used a [mysql_fetch_assoc] function

    - xFree(QUERY) - It's cleaning a memory used for query
    - xAffect() - It returns an amount of fields affected in last query
    - xyCreateDB(DBNAME, QUERY [opt.]) - It creates a database with a name provided in DBNAME argument
    - xyDropDB (DBNAME, QUERY [opt.]) - It drops a database with a name provided in DBNAME argument

    Examples:
    <?
    require_once "class.php";
    $mysql = new MySQL;
    $connect = $mysql -> xConnect();
    $execute = $mysql -> xQuery("UPDATE data SET a='1' WHERE a='2'");
    $mysql-> xFree($execute);
    $mysql -> xShutdown($connect);
    ?>
    <?
    require_once "class.php";
    $mysql = new MySQL;
    $connect = $mysql -> xConnect();
    $query = $mysql -> xQuery("SELECT FROM data WHERE id > 0");
    $fetch = xFetch($query);
    echo $fetch[0];
    $mysql -> xFree($query);
    $mysql -> xShutdown($connect);
    ?>

    IMPORTANT
    This class has built-in variables for class managing.
    An explaination is included in "class.php" file, but I'll put it here too.

    //Connection variables
    public $host='localhost';
    public $user='root';
    public $pass='';
    public $database='data';
    public $port='3306';

    //Variables for managing
    public $_setDisplayErrors=0; //This variable turns on displaying custom errors
    public $_setCritical=0; //This variable puts EVERY error into DIE() function
    public $_setCreateErrorLog=0; //This variable creates errors in _$user@$host.txt file
    public $_setLanguage='eng'; //This variable sets language for xMySQL class
    public $_setLanguageDir='lang/';
    public $_isConnected=false; //This variable returns TRUE if connection's establishes and FALSE when connection's closed
    PLEASE NOTE
    Generating errors will be included when I get more time ^_^


    CLASS.PHP
    <?
    <?
    //xMySQL Class is a free MySQL class wrote in PHP for MySQL databases managing.
    //It supports languages managing.

    //Class released on AuthWare license - it means, that whenever you change this code, basic author [Ex-Toxic] must be included.
    //Have fun!

    class MySQL
    {

    //Connection variables
    public $host='localhost';
    public $user='root';
    public $pass='';
    public $database='data';
    public $port='3306';

    //Variables for managing
    public $_setDisplayErrors=0; //This variable turns on displaying custom errors
    public $_setCritical=0; //This variable puts EVERY error into DIE() function
    public $_setCreateErrorLog=1; //This variable creates errors in _$user@$host.txt file
    public $_setLanguage='eng'; //This variable sets language for xMySQL class
    public $_setLanguageDir='lang/';
    public $_isConnected=false; //This variable returns TRUE if connection's establishes and FALSE when connection's closed


    private function _language() {
    @$file=fopen($this->_setLanguageDir.$this->_setLanguage.'.php', 'r');
    if (!$file) { die("Cannot find language file for xMySQL Class.");
    }
    }

    public function Log($text) {
    if ($this->_setCreateErrorLog==1) {
    $today = date("m.d.y H:i:s");
    @$file=fopen('error_log.txt', 'a');
    fwrite($file, '
    ['.$today.'] '.$text);
    fclose($file);
    }
    }

    public function Error($_errorIndex) {
    if ($this->_setDisplayErrors>0) {
    $this->_language();
    include($this->_setLanguageDir.$this->_setLanguage.'.php');
    if ($this->_setCritical>0) { die($Display[$_errorIndex]); } else {
    echo $Display[$_errorIndex];
    }
    $this->Log($Display[$_errorIndex]);
    }
    }

    public function xConnect() {
    if ($this->_isConnected == true) { $this->Error(0); } else {
    @$connect=mysql_connect($this->host.':'.$this->port,$user,$pass);
    if (!$connect) { $this->Error(1); } else {
    @$getDatabase=mysql_select_db($this->database, $connect);
    if(!$getDatabase) { $this->Error(2); } else { $this->isConnected = true; }
    }
    }
    }

    public function xShutdown($connection) {
    if ($this->_isConnected==true) { mysql_close($connection); } else { $this->Error(6); }
    }

    public function xQuery($queryString) {
    $this->xConnect();
    if (mysql_query($queryString)) { return true; } else { return false; $this->Error(3); }
    }

    public function xSQL($file) {
    if (@!fopen($file, "r")) { $this->Error(7); } else {
    $expl_name=explode(".", $file);
    if ($expl[1] != "sql") { $this->Error(8); } else {
    $getSQLFile = file_get_contents($file);
    if (mysql_query($getSQLFile)) { return true; } else { return false; }
    }
    }
    }

    public function xRows($query) {
    $this->xConnect();
    @$getRows=mysql_num_rows($query);
    return $getRows;
    }

    public function xFetch($query, $mode='_FA') {
    $this->xConnect();
    switch($mode)
    {
    case '_FA':
    @$exec=mysql_fetch_assoc($query);
    break;

    case '_FR':
    @$exec=mysql_fetch_row($query);
    break;

    case '_FY':
    @$exec=mysql_fetch_array($query);
    break;

    default:
    @$exec=mysql_fetch_assoc($query);
    break;
    }

    if ($exec) { return $exec; } else { $this->Error(5); }
    }

    public function xFree($query) {
    @mysql_free_result($query);
    }

    public function xAffect() {
    return mysql_affected_rows();
    }

    public function xyCreateDB($database_name, $query='')
    {
    if ($query!='') { if (mysql_create_db($database_name, $query)) { return true; } else { return false; } } else {
    if (mysql_create_db($database_name)) { return true; } else { return false; }
    }
    }

    public function xyDropDB($database_name, $query='')
    {
    if ($query != '') { if (mysql_drop_db($database_name, $query)) { return true; } else { return false; } } else {
    if (mysql_drop_db($database_name)) { return true; } else { return false; }
    }
    }
    }
    //Wrote and released by Ex-Toxic!
    ?>
    ?>
    lang/eng.php file:
    <?
    $Display = array();
    $Display['0']='Connection is already established. Another connection request cannot be accepted unless first connection exists.';
    $Display['1']='xConnect() failed: Connection to database failed. Please check your host, port, username and/or password and try to call connection one more time.';
    $Display['2']='xConnect().database_selection failed: Please check database name and its privileges.';
    $Display['3']='xQuery() failed: Query string is invalid or a table is damaged.';
    $Display['4']='xRows() failed: Cannot execute this function with provided query.';
    $Display['5']='xFetch() failed: Cannot execute this function with provided query.';
    $Display['6']='xShutdown() failed: There is no connection to close.';
    $Display['7']='xSQL() failed: Invalid file path.';
    $Display['8']='xSQL() failed: Invalid file format.';
    ?>
    Enjoy ;)
    Last edited by MaestroS; 24-11-07 at 10:21 PM.


  2. #2
    Gamma Daevius is offline
    MemberRank
    Jun 2007 Join Date
    NetherlandsLocation
    3,252Posts

    Re: [MySQL] Easy xMySQL class for beginners

    Very nice script :), good work.

  3. #3
    Account Upgraded | Title Enabled! MasterBubba is offline
    MemberRank
    Oct 2007 Join Date
    914Posts

    Re: [MySQL] Easy xMySQL class for beginners

    Works well :] Hope there is more coming out :D

  4. #4
    Member MaestroS is offline
    MemberRank
    Aug 2006 Join Date
    PolandLocation
    83Posts

    Re: [MySQL] Easy xMySQL class for beginners

    #Update
    Added: xSQL file (Thanks to Matt for an idea)
    This function "imports" an SQL file (*.sql) and queries its contents.

    Added: Logging errors (useless, but maybe someone will do something with it)

    Removed:
    variable $_setCloseError was removed due to its nonsensical existance ;-)

    I've changed something yet, but I forgot lol o.o



Advertisement