Advertisement
Guest User

Untitled

a guest
May 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.62 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class db {
  5.  
  6.     var $last_error;         // holds the last error. Usually mysql_error()
  7.     var $last_query;         // holds the last query executed.
  8.     var $row_count;          // holds the last number of rows from a select
  9.    
  10.     var $host;               // mySQL host to connect to
  11.     var $user;               // mySQL user name
  12.     var $pw;                 // mySQL password
  13.     var $db;                 // mySQL database to select
  14.    
  15.     var $db_link;            // current/last database link identifier
  16.     var $auto_slashes;       // add/strip slashes when it can
  17.    
  18.     function __construct() {
  19.    
  20.         // class constructor.  Initializations here.
  21.        
  22.         // You can also set these values in the connect() function and using
  23.         // the select_database() function.
  24.        
  25.         $this->host     = 'localhost';
  26.         $this->user     = 'PhilW';
  27.         $this->pw       = 'PhilW';
  28.         $this->db       = 'PhilW';
  29.        
  30.         $this->auto_slashes = true;
  31.        
  32.         $this->connect();
  33.    
  34.     }
  35.    
  36.     function connect( $host='', $user='', $pw='', $db='', $persistant = true ) {
  37.    
  38.         // Opens a connection to MySQL and selects the database.  If any of the
  39.         // function's parameter's are set, we want to update the class variables.  
  40.         // If they are NOT set, then we're going to use the currently existing
  41.         // class variables.
  42.         // Returns true if successful, false if there is failure.  
  43.        
  44.         if ( !empty( $host ) )  $this->host = $host;
  45.         if ( !empty( $user ) )  $this->user = $user;
  46.         if ( !empty( $pw ) )    $this->pw = $pw;
  47.        
  48.        
  49.         // Establish the connection.
  50.         if ( $persistant ) {
  51.             $this->db_link = mysql_pconnect( $this->host, $this->user, $this->pw );
  52.         } else {
  53.             $this->db_link = mysql_connect( $this->host, $this->user, $this->pw );
  54.         }
  55.        
  56.         // Check for an error establishing a connection
  57.         if (!$this->db_link) {
  58.             $this->last_error = mysql_error();
  59.             return false;
  60.         }
  61.    
  62.         // Select the database
  63.         if ( !$this->select_db( $db ) ) {
  64.             return false;
  65.         } else {
  66.             return $this->db_link;  // success
  67.         }
  68.        
  69.     }
  70.    
  71.     function select_db( $db = '' ) {
  72.    
  73.         // Selects the database for use.  If the function's $db parameter is
  74.         // passed to the function then the class variable will be updated.
  75.        
  76.         if ( !empty( $db ) ) $this->db = $db;
  77.        
  78.         if ( !mysql_select_db( $this->db ) ) {
  79.             $this->last_error = mysql_error();
  80.             return false;
  81.         } else {       
  82.             return true;
  83.         }
  84.        
  85.     }
  86.    
  87.     function select($sql) {
  88.    
  89.         // Performs an SQL query and returns the result pointer or false
  90.         // if there is an error.
  91.        
  92.         $this->last_query = $sql;
  93.        
  94.         $r = mysql_query( $sql );
  95.         if ( !$r ) {
  96.             $this->last_error = mysql_error();
  97.             return false;
  98.         } else {
  99.             $this->row_count = mysql_num_rows($r);
  100.             return $r;
  101.         }
  102.        
  103.     }
  104.    
  105.     function get_row( $result, $type = 'MYSQL_BOTH' ) {
  106.    
  107.         // Returns a row of data from the query result.  You would use this
  108.         // function in place of something like while($row=mysql_fetch_array($r)).
  109.         // Instead you would have while($row = $db->get_row($r)) The
  110.         // main reason you would want to use this instead is to utilize the
  111.         // auto_slashes feature.
  112.        
  113.         if ( !$result ) {
  114.             $this->last_error = "Invalid resource identifier passed to get_row() function.";
  115.             return false;  
  116.         }
  117.        
  118.         if ( $type == 'MYSQL_ASSOC' )   $row = mysql_fetch_array( $result, MYSQL_ASSOC );
  119.         if ( $type == 'MYSQL_NUM' )     $row = mysql_fetch_array( $result, MYSQL_NUM );
  120.         if ( $type == 'MYSQL_BOTH' )    $row = mysql_fetch_array( $result, MYSQL_BOTH );
  121.        
  122.         if (!$row) return false;
  123.        
  124.         if ($this->auto_slashes) {
  125.        
  126.             // strip all slashes out of row...
  127.             foreach ($row as $key => $value) {
  128.                 $row[$key] = stripslashes($value);
  129.             }
  130.            
  131.         } else {
  132.             return $row;
  133.         }
  134.    
  135.     }
  136.    
  137.     function insert_sql($sql) {
  138.    
  139.         // Inserts data in the database via SQL query.
  140.         // Returns the id of the insert or true if there is not auto_increment
  141.         // column in the table.  Returns false if there is an error.      
  142.        
  143.         $this->last_query = $sql;
  144.        
  145.         $r = mysql_query( $sql );
  146.         if ( !$r ) {
  147.             $this->last_error = mysql_error();
  148.             return false;
  149.         }
  150.        
  151.         $id = mysql_insert_id();
  152.         if ($id == 0) {
  153.             return true;
  154.         } else {
  155.             return $id;
  156.         }
  157.        
  158.     }
  159.    
  160.     function update_sql($sql) {
  161.    
  162.         // Updates data in the database via SQL query.
  163.         // Returns the number or row affected or true if no rows needed the update.
  164.         // Returns false if there is an error.
  165.        
  166.         $this->last_query = $sql;
  167.        
  168.         $r = mysql_query( $sql );
  169.         if ( !$r ) {
  170.             $this->last_error = mysql_error();
  171.             return false;
  172.         }
  173.        
  174.         $rows = mysql_affected_rows();
  175.         if ($rows == 0){
  176.             return true;  // no rows were updated
  177.         } else {
  178.             return $rows;
  179.         }
  180.    
  181.     }
  182.    
  183. }
  184.    
  185. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement