[REL] Simple MySQL Class [PHP / MySQL]

Results 1 to 2 of 2
  1. #1
    Member FreedomSaint is offline
    MemberRank
    Jul 2010 Join Date
    PrisonLocation
    83Posts

    happy [REL] Simple MySQL Class [PHP / MySQL]

    Hey guys.
    Got bored so I wrote a quick MySQL class. It's basic but it works, and it also has it's own error class too. Please use constructive criticism!

    MySQL.class.php -
    Spoiler:

    PHP Code:
    <?php
    class Database extends Error
    {
        private 
    $host;
        private 
    $username;
        private 
    $password;
        private 
    $database;
        
        public function 
    __construct ($h$u$p$db)
        {
            
    $this->host $h
            $this
    ->username $u;
            
    $this->password $p;
            
    $this->database $db;
            
            
    $connection mysql_connect($this->host$this->username$this->password);
            
    $connection mysql_select_db($this->database$connection);
            if(
    $connection)
            {
                return 
    $connection;
            }
            else
            {
                die(
    Error::err(1));
            }
        }
        public function 
    _query($_QUERY)
        {
            
    $query mysql_query($_QUERY);
            if(
    $query)
            {
                return 
    $query;
            }
            else
            {
                die(
    Error::err(2$_QUERY));
            }
        }
        public function 
    _numRows($_QUERY)
        {
            
    $num mysql_num_rows($_QUERY);
            if(!empty(
    $_QUERY))
            {
                return 
    $num;
            }
            else
            {
                die(
    Error::err(3));
            }
        }
        public function 
    _createArray($_QUERY)
        {
            
    $array mysql_fetch_array($_QUERY);
            if(!empty(
    $_QUERY))
            {
                return 
    $array;
            }
            else
            {
                die(
    Error::err(4));
            }
        }
    }


    Error.class.php -
    Spoiler:

    PHP Code:

    <?php
    class Error
    {
        public function 
    err($errid$param false)
        {
            
    $err = (int)$errid;
            switch(
    $err)
            {
                default:
                    
    $error 'Unexpected error, please contact the system administrator #0';
                break;
                case 
    1:
                    
    $error 'Failed to execute the following query: ' $param ' #1';
                break;
                case 
    2:
                    
    $error 'Failed to select the Database from the MySQL server! #2';
                break;
                case 
    3:
                    
    $error 'Failed to find the number of rows! #3';
                break;
                case 
    4:
                    
    $error 'Failed to create an array from MySQL results! #4';
                break;
                
            }
            
    $front '<font color="red"><b>Error:</b>';
            
    $back '</font>';
            
    $error $front $error $back;
            return 
    $error;
            
        }

    }


    Example of use:
    Spoiler:

    PHP Code:
    <?php
    /*
     Example of the MySQL class created by Leon H.
     
     #############################################
     
     This isn't something you should use for a live
     site, it's just an example!
    */
        
    header("Content-Type: text/plain"); // Set the content type to Plain Text so it displays the full array correctly.
        
        
    require "MySQL.class.php"// Require the Database class file
        
    require "Error.class.php"// Require the Error class file
        
        
    $host 'localhost'// Set your Database host var
        
    $username 'root'// Set your Database username var
        
    $password 'password'// Set your Database password var
        
    $database 'database'// Set your Database name var
        
        
        
    $db = new Database($host$username$password$database); // Create the Database instance and connect to the DB server
        
        
    $query $db->_query("SELECT * FROM `news` LIMIT 3"); // Execute a basic query.
        
    $array $db->_createArray($query); // Turn the MySQL results into an array
        
        
    print_r($array); // Print out the full array.
        
    ?>

    Hope you like it and build upon it, and remember it's basic!

    Yours,
    FS.
    Last edited by FreedomSaint; 04-09-11 at 04:32 PM.


  2. #2
    Proficient Member viroware is offline
    MemberRank
    Jan 2011 Join Date
    152Posts

    Re: [REL] Simple MySQL Class [PHP / MySQL]

    I think its to basic, You should add some more basic things like making it safe against mysql injections ect. Just keep adding small things to it and it safe a lot of time for anyone who uses it.



Advertisement