WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes Click here to Update Your Picture
leaping langoor
Date : Aug 22nd 2004
Grade : 2 of 5 (graded 6 times)
Viewed : 9903
File : No file for this code example.
Images : No Images for this code example.
Search : More code by leaping langoor
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Ok so this is one of my latest creations. This example is not too complicated to given example for. Just in case there is a small one at the end demonstrating just one function. You can use the rest in the same way.

Author... leapinglangoor [ leapinglangoor@yahoo.co.in ]
Updated.. 21 Aug 2004
Version.. 2.1
<?php
class MySQLHandler {

 
// Change these variables to your own database settings
   
var $DATABASE = 'mysql';
  var
$USERNAME = 'root';
  var
$PASSWORD = '';
  var
$SERVER = 'localhost';

    var
$LOGFILE = "c:/mysql.log"; // full path to debug LOGFILE. Use only in debug mode!
 
var $LOGGING = false; // debug on or off
 
var $SHOW_ERRORS = true; // output errors. true/false
 
var $USE_PERMANENT_CONNECTION = false;

 
// Do not change the variables below
 
var $CONNECTION;
    var
$FILE_HANDLER;
    var
$ERROR_MSG = '';

###########################################
# Function:    init
# Parameters:  N/A
# Return Type: boolean
# Description: initiates the MySQL Handler
###########################################
 
function init() {
   
$this->logfile_init();
    if (
$this->OpenConnection()) {
      return
true;
    } else {
      return
false;
        }
    }

###########################################
# Function:    OpenConnection
# Parameters:  N/A
# Return Type: boolean
# Description: connects to the database
###########################################
   
function OpenConnection()    {
    if (
$this->USE_PERMANENT_CONNECTION) {
     
$conn = mysql_pconnect($this->SERVER,$this->USERNAME,$this->PASSWORD);
    } else {
     
$conn = mysql_connect($this->SERVER,$this->USERNAME,$this->PASSWORD);
    }
        if ((!
$conn) || (!mysql_select_db($this->DATABASE,$conn))) {
     
$this->ERROR_MSG = "\r\n" . "Unable to connect to database - " . date('H:i:s');
     
$this->debug();
      return
false;
    } else {
         
$this->CONNECTION = $conn;
          return
true;
    }
    }

###########################################
# Function:    CloseConnection
# Parameters:  N/A
# Return Type: boolean
# Description: closes connection to the database
###########################################
   
function CloseConnection() {
      if (
mysql_close($this->CONNECTION)) {
      return
true;
    } else {
     
$this->ERROR_MSG = "\r\n" . "Unable to close database connection - " . date('H:i:s');
     
$this->debug();
      return
false;
    }
    }

###########################################
# Function:    logfile_init
# Parameters:  N/A
# Return Type: N/A
# Description: initiates the logfile
###########################################
   
function logfile_init() {
    if (
$this->LOGGING) {
     
$this->FILE_HANDLER = fopen($this->LOGFILE,'a') ;
       
$this->debug();
    }
    }
   
###########################################
# Function:    logfile_close
# Parameters:  N/A
# Return Type: N/A
# Description: closes the logfile
###########################################
   
function logfile_close() {
    if (
$this->LOGGING) {
          if (
$this->FILE_HANDLER) {
           
fclose($this->FILE_HANDLER) ;
        }
    }
    }

###########################################
# Function:    debug
# Parameters:  N/A
# Return Type: N/A
# Description: logs and displays errors
###########################################
 
function debug() {
    if (
$this->SHOW_ERRORS) {
      echo
$this->ERROR_MSG;
    }
    if (
$this->LOGGING) {
          if (
$this->FILE_HANDLER) {
             
fwrite($this->FILE_HANDLER,$this->ERROR_MSG);
          } else {
              return
false;
          }
    }
    }

###########################################
# Function:    Insert
# Parameters:  sql : string
# Return Type: integer
# Description: executes a INSERT statement and returns the INSERT ID
###########################################
   
function Insert($sql) {
        if ((empty(
$sql)) || (!eregi("^insert",$sql)) || (empty($this->CONNECTION))) {
     
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not an INSERT - " . date('H:i:s');
     
$this->debug();
      return
false;
    } else {
         
$conn = $this->CONNECTION;
         
$results = mysql_query($sql,$conn);
          if (!
$results) {
       
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
       
$this->debug();
        return
false;
      } else {
           
$result = mysql_insert_id();
            return
$result;
      }
    }
    }

###########################################
# Function:    Select
# Parameters:  sql : string
# Return Type: array
# Description: executes a SELECT statement and returns a
#              multidimensional array containing the results
#              array[row][fieldname/fieldindex]
###########################################
   
function Select($sql)    {
        if ((empty(
$sql)) || (!eregi("^select",$sql)) || (empty($this->CONNECTION))) {
     
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a SELECT - " . date('H:i:s');
     
$this->debug();
      return
false;
    } else {
         
$conn = $this->CONNECTION;
         
$results = mysql_query($sql,$conn);
          if ((!
$results) || (empty($results))) {
       
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
       
$this->debug();
        return
false;
      } else {
       
$i = 0;
       
$data = array();
        while (
$row = mysql_fetch_array($results)) {
           
$data[$i] = $row;
           
$i++;
        }
       
mysql_free_result($results);
        return
$data;
      }
    }
    }

###########################################
# Function:    Update
# Parameters:  sql : string
# Return Type: integer
# Description: executes a UPDATE statement
#              and returns number of affected rows
###########################################
   
function Update($sql)    {
        if ((empty(
$sql)) || (!eregi("^update",$sql)) || (empty($this->CONNECTION))) {
     
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not an UPDATE - " . date('H:i:s');
     
$this->debug();
      return
false;
    } else {
         
$conn = $this->CONNECTION;
         
$results = mysql_query($sql,$conn);
          if (!
$results) {
       
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
       
$this->debug();
        return
false;
      } else {
            return
mysql_affected_rows();
      }
    }
    }
 
###########################################
# Function:    Replace
# Parameters:  sql : string
# Return Type: boolean
# Description: executes a REPLACE statement
###########################################
   
function Replace($sql) {
        if ((empty(
$sql)) || (!eregi("^replace",$sql)) || (empty($this->CONNECTION))) {
     
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a REPLACE - " . date('H:i:s');
     
$this->debug();
      return
false;
    } else {
         
$conn = $this->CONNECTION;
         
$results = mysql_query($sql,$conn);
          if (!
$results) {
       
$this->ERROR_MSG = "\r\n" . "Error in SQL Statement : ($sql) - " . date('H:i:s');
       
$this->debug();
        return
false;
      } else {
            return
true;
      }
    }
    } 

###########################################
# Function:    Delete
# Parameters:  sql : string
# Return Type: boolean
# Description: executes a DELETE statement
###########################################
   
function Delete($sql)    {
        if ((empty(
$sql)) || (!eregi("^delete",$sql)) || (empty($this->CONNECTION))) {
     
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> or not a DELETE - " . date('H:i:s');
     
$this->debug();
      return
false;
    } else {
         
$conn = $this->CONNECTION;
         
$results = mysql_query($sql,$conn);
          if (!
$results) {
       
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
       
$this->debug();
        return
false;
      } else {
            return
true;
      }
    }
    }
 
###########################################
# Function:    Query
# Parameters:  sql : string
# Return Type: boolean
# Description: executes any SQL Query statement
###########################################
   
function Query($sql)    {
        if ((empty(
$sql)) || (empty($this->CONNECTION))) {
     
$this->ERROR_MSG = "\r\n" . "SQL Statement is <code>null</code> - " . date('H:i:s');
     
$this->debug();
      return
false;
    } else {
         
$conn = $this->CONNECTION;
         
$results = mysql_query($sql,$conn);
          if (!
$results) {
       
$this->ERROR_MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
       
$this->debug();
        return
false;
      } else {
            return
true;
      }
    }
    }
}
?>


----------------------------------------------------------------
----------------------------------------------------------------


example1.php:
<?php

include('MySQLHandler.php');

//Setup the class
//Be carefule wit the cases
$db = new MySQLHandler;

//Initialise
$db -> init();
$db -> OpenConnection();
$db -> logfile_init();

$sql = "SELECT * FROM `users`"
$result = $db -> Select($sql);

/* execute the $result the way you want */




Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs