WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 20766
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  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 */

/* close all
$db -> logfile_close();
$db -> CloseConnection();

?>



Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
PHP Transfer data from text file to Mysql Table
Categories : PHP, PHP Classes, Filesystem, Databases, MySQL
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Mssql database Manager
Categories : PHP, Databases, MS SQL Server, Classes and Objects, PHP Classes
PHP CLASS for ORACLE (database connectivity)
Categories : PHP, PHP Classes, Classes and Objects, Databases, Oracle
Link Manager for Link Exchangers
Categories : PHP, PHP Classes, Databases, MySQL, CURL
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL