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 : Database Class - This class is designed to handle SQL transations from databases, currently only MySQL Supported.
Categories : PHP, PHP Classes, Databases, MySQL Click here to Update Your Picture
James Mitchell
Date : May 21st 2007
Grade : 5 of 5 (graded 2 times)
Viewed : 3415
File : 4641.php
Images : No Images for this code example.
Search : More code by James Mitchell
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 

<?php
/**
* This is a database class used to do queries to a database using SQL.
* @author ReDucTor <james_mitchell_au@yahoo.com.au>
* @version 1
*/

class DB {
    var
$type;
    var
$host;
    var
$username;
    var
$password;
    var
$database;
    var
$conn;
    var
$errno=false;
    var
$error=false;

    function
DB($type,$host,$username,$password,$database) {
       
$this->type = $type;
       
$this->username = $username;
       
$this->password = $password;
       
$this->database = $database;
       
$this->host = $host;
        if(
$type=='mysql') {
           
$this->conn = @mysql_connect($host,$username,$password);
            if(
$this->conn == false) {
               
$this->errno = mysql_errno();
               
$this->error = mysql_error();
            }
           
$result = @mysql_select_db($this->database,$this->conn);
            if(
$result == false) {
               
$this->errno = mysql_errno();
               
$this->error = mysql_error();
            }
        } else {
           
$this->errno = -1;
           
$this->error = 'Invalid Database Type';
        }
    }

   
/**
    * @function insQuery
    * Used for INSERT query,
    * returns false if failed, and returns insert id on success
    */
   
function insQuery($sql) {
       
$this->resetError();
        if(
$this->type=='mysql') {
           
$q = @mysql_query($sql,$this->conn);
            if(
$q == false) {
               
$this->errno = mysql_errno($this->conn);
               
$this->error = mysql_error($this->conn);
                return
false;
            } else {
                return
mysql_insert_id($this->conn);
            }
        }
    }

   
/**
    * @function selQuery
    * Used for SELECT query of multiple records
    * returns resource to use, or false if failed.
    */
   
function selQuery($sql) {
       
$this->resetError();
        if(
$this->type=='mysql') {
           
$q = @mysql_query($sql,$this->conn);
            if(
$q == false) {
               
$this->errno = mysql_errno($this->conn);
               
$this->error = mysql_error($this->conn);
                return
false;
            } else {
                return
$q;
            }
        }
    }

   
/**
    * @function fieldQuery
    * Used for SELECT query which involves a single field, in a single row
    * returns the first field, in the first row (used for things like COUNT(*))
    */
   
function fieldQuery($sql) {
       
$this->resetError();
        if(
$this->type=='mysql') {
           
$q = @mysql_query($sql,$this->conn);
            if (
$q == false) {
               
$this->errno = mysql_errno($this->conn);
               
$this->error = mysql_error($this->conn);
                return
false;
            } else {
                return
mysql_result($q,0,0);
            }
        }
    }

   
/**
    * @function affectQuery
    * Used for UPDATE/REPLACE/DELETE on the database
    * returns false if there was an error, or returns the number of affected rows if success
    */
   
function affectQuery($sql) {
       
$this->resetError();
        if(
$this->type=='mysql') {
           
$q = @mysql_query($sql,$this->conn);
            if (
$q == false) {
               
$this->errno = mysql_errno($this->conn);
               
$this->error = mysql_error($this->conn);
            } else {
                return
mysql_affected_rows($this->conn);
            }
        }
    }

   
/**
    * @function fetch
    * Used to fetch an associative array of a row
    * returns array, or false if there is no more rows
    */
   
function fetch($resource) {
        if(
$this->type=='mysql') {
            return
mysql_fetch_assoc($resource);
        }
    }

   
/**
    * @function numrows
    * Used to get the number of rows from a query
    * returns the numbers from a query or false on failure
    */
   
function numrows($resource) {
        if(
$this->type=='mysql') {
            return
mysql_num_rows($resource);
        }
    }

   
/**
    * @function free
    * free the result resource
    */
   
function free($resource) {
        if(
$this->type=='mysql') {
            return @
mysql_free_result($resource);
        }
    }

   
/**
    * @function errno
    * returns the error number of the last error
    */
   
function errno() {
        return
$this->errno;
    }

   
/**
    * @functoin error
    * returns the error description of the last error
    */
   
function error() {
        return
$this->error;
    }

   
/**
    * @function resetError
    * resets the error values, for a new query
    */
   
function resetError() {
       
$this->errno = false;
       
$this->error = false;
    }

}
?>



Usage Example
<?php
$db
= new DB($config['sql_type'], $config['sql_host'], $config['sql_user'], $config['sql_pass'], $config['sql_dbase']);
if(
$db->error()) {
  die(
'Failed to connect to database: '.$db->error());
}
$q = $db->selQuery('SELECT field1,field2 FROM table WHERE id='.intval($id));
if(
$q == false) {
  die(
$db->error());
}
if(
$db->numRows($q) == 0) {
  die(
'No data');
}
var_dump($db->fetch($q));
?>



Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes
phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP
DBXML- A Class to backup databases in XML Format using web interface
Categories : PHP, PHP Classes, Databases, MySQL, XML
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs
This class splits the results of the query into multiple pages like what the search engine does.
Categories : PHP Classes, PHP, MySQL, Databases
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 database class
Categories : PHP, MySQL, Databases, PHP Classes
TAB_STRUCT Class: Is supporting Class for the DBXML Class
Categories : PHP, PHP Classes, MySQL, XML, Databases
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
Link Manager for Link Exchangers
Categories : PHP, PHP Classes, Databases, MySQL, CURL
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL