NameSilo

MySQL Class

Spaceship Spaceship
Watch
Got a bit bored today (nothing on TV at Christmas huh) so i wrote a mysql class to share.

Enjoy, add to it...use it...do whatever you want.
Note this is untested, i don't see any problems but i haven't yet got around to trying it.


PHP:
<?php
/*   ********** Mysql class **********
    Written on 25th December 2006 (nothing on tv @ christmas)
    
    Written by: Matt Jewell
                http://3bigtrees.com
                
    Feel free to modify and redistribute,
    But please keep the written by lines intact.

    Compatible with PHP: >=5.0.0
    
    Additions/Modifcations:
    
    [Name]                    [Details]
    Matt                      First version written
    
    */


class mysql
{
      protected    $con;                 // Holds current connection info
      private      $n = 0;            // Holds number of queries
      
      
    //-----------------------------------------
    # mysql_connect, mysql_select_db
    //-----------------------------------------      
      
    public function __construct ($host = 'localhost', $un, $pw, $db)
    {
            $this -> con = @mysql_connect($host, $un, $pw);
            
            if($this -> con)
            {
                $database = @mysql_select_db($db, $this -> con);
                                  
                if(!$database)
                {
                    trigger_error('Error ' . mysql_errno() . ' : ' . mysql_error(), E_USER_WARNING);
                }
            }

            register_shutdown_function(array(&$this, "__destruct"));
      }
      
      
    //-----------------------------------------
    # mysql query: mysql_query(sql, ident)
    //-----------------------------------------
    
      public function query ($sql)
      {
            $query = @mysql_query($sql, $this -> con);
                
            if(!$query)
            {
                 trigger_error('Error ' . mysql_errno() . ' : ' . mysql_error(), E_USER_WARNING);
            }
            else
            {
                $this -> n++;
                return $query;
            }

      }
      
    //-----------------------------------------
    # mysql fetch array: mysql_fetch_array(query, ident)
    # Type:
    # MYSQL_ASSOC: default, returns as associative array.
    # MYSQL_NUM: Return enumerated array
    //-----------------------------------------
    
    public function fetcharray ($query, $type = MYSQL_ASSOC)
    {
          return mysql_fetch_array($query, $type);  

    }
    
      
    //-----------------------------------------
    # mysql num rows() // use COUNT query instead to improve
    //-----------------------------------------
    
      function numrows($query)
      {
            return mysql_num_rows($query);
      }

    //-----------------------------------------
    # Insert into database
    # Use array for second argument
    # e.g. arrray('fieldname' => 'value', 'field2' => 'value2')
    # e.g. insert('blog', array('title' => 'hello', 'body' => 'content here'));
    //-----------------------------------------      
    
    public function insert($table, $fields)
    {
         if(!sizeof($fields) > 0 || !is_array($fields))
         {
              trigger_error('Second argumenty for insert must be an array', E_USER_NOTICE);
         }
         else
         {
          
              foreach($fields as $key => $value)
              {
                   $field_names[]     = "`{$key}`";
                   $field_values[]    = "'{$value}'";
              }
              
              $this -> query('INSERT INTO `' . $table . '` (' . implode(', ', $field_names) . ') VALUES (' . implode(', ', $field_values) . ')');
         }
    }
        
              

    //-----------------------------------------
    # return the number of queries used
    //-----------------------------------------
    
    public function countcon ()
    {
         return $this -> n;
    }
    
    
    //-----------------------------------------
    # return mysql server information: mysql_get_seerver_info
    //-----------------------------------------
    
    public function serverinfo ()
    {
         return mysql_get_server_info($this -> con);
         // return sprintf("%s", mysql_get_server_info($this -> con));
    }
    
    
    //-----------------------------------------
    # Return a list of all tables in given database
    # SHOW TABLES FROM database
    //-----------------------------------------
    
    public function list_tables ($database)
    {
         if(count($database) > 0)
         {
          
             return $this -> query('SHOW TABLES FROM `' . $database . '`');
            
            //     Example of use:
            #    while ($row = mysql_fetch_row($result, MYSQL_NUM))
            #    {
               #        echo "Table: {$row[0]}";
            #    }
            //---------------------
            
        }
    }
    
    
    //-----------------------------------------
    # Mysql stats: mysql_stat(ident)
    # Returns an enumerated array of connection statistics
    # $return_array: Tells the function if it should return an array of results or not
    # Standard result:
    #        Uptime: 357 Threads: 1 Questions: 140 Slow queries: 0 ...etc
    # Put into an array:
    #        Array
    #        (
    #            [0] => Uptime: 5380
    #            [1] => Threads: 2
    #            [2] => Questions: 1321299
    #            [3] => Slow queries: 0
    #            ...etc
    #        )
    //-----------------------------------------    
    
    public function mysql_stats ($return_array = false)
    {
         if($return_array == true)
         {
              return explode('  ', mysql_stat($this -> con));
         }
         else
         {
              return mysql_stat($this -> con);
         }
    }
        
    
    //-----------------------------------------
    # Disconnect from mysql: mysql_close(ident)
    //-----------------------------------------    
    
    public function __destruct ()
    {
         mysql_close($this -> con);
    }
}


// Create a new instance of the class
$db = new mysql('localhost', 'username', 'password', 'database');

?>


+Rep as always is appreciated if you use this. Consider it my x-mas present lol

Matt. :santa:
 
Last edited:
1
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Thanks
 
0
•••
I'm a bit confused about this.
From what I can gather, this script is used to execute a query on adb, then return the stats on the query....

I am knew to php, and slightly confused lol.

Seems a good script though.
 
0
•••
Lol, if you're new to php then you will need to read up about classes (OOP) to understand it a bit more :)

http://php.net/oop
 
0
•••
Great class.

However, I suggest using adodb (http://adodb.sourceforge.net/) instead.
It can access multiple types of databases transparently with little change of code.
 
1
•••
jerometan said:
Great class.

However, I suggest using adodb (http://adodb.sourceforge.net/) instead.
It can access multiple types of databases transparently with little change of code.
Thanks for the adodb link :)
I didn't knew about this os class yet, but it is great!
Rep+
 
0
•••
just had a quick look through the class and noticed you have the following line:-

PHP:
echo 'INSERT INTO `' . $table . '` (' . implode(', ', $field_names) . ') VALUES (' . implode(', ', $field_values) . ')';

Surely that should be taken out of the class as I presume you had it there for debugging.
 
0
•••
OOOPs

Thanks filth@flexiwebhost, good eyes ;)
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back