NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page MySQL Class

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
7 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 12-25-2006, 08:37 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship

MySQL Class


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 Code:
<?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++;
????: NamePros.com http://www.namepros.com/code/273050-mysql-class.html
                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) > || !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));
????: NamePros.com http://www.namepros.com/showthread.php?t=273050
         }
         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.
Last edited by Matthew.; 01-03-2007 at 11:21 AM.
Matthew. is offline  
Old 12-26-2006, 01:43 PM   #2 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 734
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



Thanks
DylanButler is offline  
Old 12-31-2006, 04:24 AM   #3 (permalink)
Account Suspended
 
Alex.'s Avatar
Join Date: Nov 2006
Location: Uk
Posts: 601
Alex. is on a distinguished road
 


Ethan Allen Fund Third World Education
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.
Alex. is offline  
Old 12-31-2006, 05:32 AM THREAD STARTER               #4 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship
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
Matthew. is offline  
Old 12-31-2006, 07:52 PM   #5 (permalink)
NamePros Regular
Join Date: Feb 2006
Posts: 584
jerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to alljerometan is a name known to all
 



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.
jerometan is offline  
Old 01-01-2007, 02:14 AM   #6 (permalink)
Account Suspended
 
DeViAnThans3's Avatar
Join Date: Apr 2006
Location: Belgium
Posts: 442
DeViAnThans3 is a name known to allDeViAnThans3 is a name known to allDeViAnThans3 is a name known to allDeViAnThans3 is a name known to allDeViAnThans3 is a name known to allDeViAnThans3 is a name known to all
 



Originally Posted by jerometan
Great class.
????: NamePros.com http://www.namepros.com/showthread.php?t=273050

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+
DeViAnThans3 is offline  
Old 01-01-2007, 09:37 AM   #7 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
just had a quick look through the class and noticed you have the following line:-

PHP Code:
echo 'INSERT INTO `' $table '` (' implode(', '$field_names) . ') VALUES (' implode(', '$field_values) . ')'
????: NamePros.com http://www.namepros.com/showthread.php?t=273050
Surely that should be taken out of the class as I presume you had it there for debugging.
Peter is offline  
Old 01-01-2007, 09:40 AM THREAD STARTER               #8 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship
OOOPs

Thanks filth@flexiwebhost, good eyes
Matthew. is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 03:16 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger