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 "driver" class
Categories : PHP, MySQL, Databases, PHP Classes Click here to Update Your Picture
Ben Barnett
Date : Feb 27th 2005
Grade : 3 of 5 (graded 3 times)
Viewed : 3756
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Ben Barnett
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php
/**
* A "driver" for mySQL databases
* Contains code for connecting, inserting, selecting and
* generally doing lots of stuff to the database. By doing
* this you have an easy, single point of access for dealing
* with mySQL functions, and can also count the number of queries
* used per instance of this class. It has let me tidy up my code
* no end.
* To use it, instantiate the class and call ::db_connect(), eg:
* $sql = new sql_driver();
* $sql->db_connect();
*/
class sql_driver
{
   
/**
    * Connection details array
    */
   
var $conn_details = array(
                           
'db_db' => 'DATABASE_NAME',
                           
'db_user' => 'USER_NAME',
                           
'db_pass' => 'PASSWORD',
                           
'db_host' => 'localhost',
                           
'db_port' => '',
                            );
   
   
/**
    * Whether or not to use persistant connections (1=yes, 0=no)
    */
   
var $persistant = 0;
   
   
/**
    * Connection identifier
    */
   
var $conn = '';
   
   
/**
    * Result identifier
    */
   
var $result = '';
   
   
/**
    * Number of queries performed
    */
   
var $query_count = 0;
   
   
/**
    * Result row
    */
   
var $result_row = array();
   
   
/**
    * Connects to the database
    *
    * @return void
    */
   
function db_connect()
    {
        if (
$this->persistant)
            {
           
$this->conn = @mysql_pconnect(    $this->conn_details['db_host'],
                                           
$this->conn_details['db_user'],
                                           
$this->conn_details['db_pass']);
            }
        else
            {
           
$this->conn = @mysql_connect(    $this->conn_details['db_host'],
                                           
$this->conn_details['db_user'],
                                           
$this->conn_details['db_pass']);
            }
   
   
        if (!
$this->conn)
            {
            echo
'Error: unable to connect to database server.';
            }
        if (!@
mysql_select_db($this->conn_details['db_db'], $this->conn))
            {
            echo
'Error: unable to connect to specified database.';
            }
   
    }
   
   
   
   
/**
    * Carries out query
    *
    * @return resource mySQL result resource
    */
   
function query($query)
        {
       
$this->result = mysql_query($query, $this->conn);
       
$this->query_count++;
        return
$this->result;
        }
   
   
   
   
/**
    * Returns an associative array of each row
    *
    * Returns an associative array of a row from the result in parameters. If no
    * parameter is present, it returns a row from the last result.
    *
    * @return array An array of the fetched row.
    */
   
function get_row()
        {
       
$this->result_row = array();
       
$this->result_row = mysql_fetch_assoc($this->result);
        return
$this->result_row;
        }
   
   
   
   
/**
    * Returns the number of rows in a result
    *
    * @return integer Number of rows in the result.
    */
   
function get_num_rows()
        {
        return
mysql_num_rows($this->result);
        }
   
   

   
/**
    * Returns the number of rows affected by the last insert, update or delete query.
    *
    * The PHP manual says that this will not be reliable if the updated value is the
    * same as the pre-update value.
    *
    * @return integer The number of affected rows.
    */
   
function get_affected_rows()
        {
        return
mysql_affected_rows($this->conn);
        }
       
       
       
   
/**
    * Returns the number of queries performed.
    *
    * @return integer The number of queries performed.
    */
   
function get_query_count()
        {
        return
$this->query_count;
        }
       
       
       
   
/**
    * Frees the result resource from memory
    */
   
function free_result()
        {
       
mysql_free_result($this->result);
        }
       
       
   
/**
    * Closes the mySQL connection
    */   
   
function db_close()
        {
       
mysql_close($this->conn);
        }
}
// End sql_driver class
?>



usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
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
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
Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
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