View Single Post
  #1 (permalink)  
Old 08-13-2009, 10:10 AM
kgun's Avatar
kgun kgun is offline
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 6,111
kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10kgun RepRank 10
Lightbulb A walk in the garden part 1.

Ok let us start.

After a lot of frustration, I finally made up my mind to start this walk in the garden part 1 ... n threads.

The frustration is about database connection.

1. The problem

We have a lot of database platforms from the popular MySQL platform to the advanced Oracle enterprise database platform. And if you use the simple MySQL platform, there are more than one way to connect to the database. Here are 4 different ways to do it in PHP.
  1. PHP: mysql_connect - Manual
  2. MySQLi (PHP MySQL improved)
  3. PHP: PDO - Manual
  4. External libraries like pear DB_DataObject and pear MDB2
2. The end result of this first thread.
  1. Make a general MySQL database connection class
  2. (that may be extended to other database platforms like PostgreSQL and ... time will show)

3. Related links

A soft introduction to object oriented programming

PHP 6 on the radar

Building Object-Oriented Database Interfaces in PHP: Processing Data through Data Access Objects

4. The first walk.

Here is the first walk without further comments, since the code is fairly well documented. Fine if you test the code and find any errors / bugs. I have found none so long.

File: DbConnect.php

PHP Code:
<?php
/**
* PHP MySQL Database Connection Class
* @access public
*/
class DbConnect {
     
/**
    * MySQL server hostname
    * @access private
    * @var string
    */
    
private $host;
    
    
/**
    * MySQL username
    * @access private
    * @var string
    */
    
private $dbUser;
    
    
/**
    * MySQL user's password
    * @access private
    * @var string
    */
     
private $dbPass;

    
/**
    * Name of database to use
    * @access private
    * @var string
    */
     
private $dbName;

    
/**
    * MySQL Resource link identifier stored here
    * @access private
    * @var string
    */
    
private $dbConn;

    
/**
    * Stores error messages for connection errors
    * @access private
    * @var string
    */
    
private $connectError;

    
/**
    * MySQL constructor
    * @param string host (MySQL server hostname)
    * @param string dbUser (MySQL User Name)
    * @param string dbPass (MySQL User Password)
    * @param string dbName (Database to select)
    * @access public
    */
     
public function __construct($host,$dbUser,$dbPass,$dbName) {
        
$this->host=$host;
        
$this->dbUser=$dbUser;
        
$this->dbPass=$dbPass;
        
$this->dbName=$dbName;
        
$this->connectToDb();
    }
    
    
/**
    * Establishes connection to DbConnect.MySQL and selects a database
    * @return void
    * @access private
    */
    
private function connectToDb () {
        
// Make connection to MySQL server
        
if (!$this->dbConn = @mysql_connect($this->host,
                                      
$this->dbUser,
                                      
$this->dbPass)) {
            
trigger_error('Could not connect to server');
            
$this->connectError=true;
        
// Select database
        
} else if ( !@mysql_select_db($this->dbName,$this->dbConn) ) {
            
trigger_error('Could not select database');
            
$this->connectError=true;
        }
    }

    
/**
    * Checks for MySQL errors
    * @return boolean
    * @access public
    */
    
public function isError () {
        if ( 
$this->connectError )
            return 
true;
        
$error=mysql_error ($this->dbConn);
        if ( empty (
$error) )
            return 
false;
        else
            return 
true;
    }
}
?>
5. Use

PHP Code:
require_once('DbConnect.php');
$host='***';   // Hostname of MySQL server
$dbUser='***';    // Username for MySQL
$dbPass='***';   // Password for user
$dbName='***';  // Database name
// Instantiate MySQL connection
$db=new DbConnect($host,$dbUser,$dbPass,$dbName); 

6. The next walk or exercise for you.


Test for MySQL PHP functions and improve connection.

Hint:

PHP Code:
if (function_exists('mysqli_connect')) {
Code here.    

But remember, conditional code like the one above, is a clear indication of code duplication and since minimalism is an essential part of good OOP design, it is an indication of
  1. An (abstract) base (parent) class that extends functionality through
  2. inheritance.
  3. Last but not least. Don't forget to program to an interface and not to an implementation.
So try to solve the above problem yourself before I present my solution. I can not promise when.
__________________
Mini Network:: Financial information at your fingertips
Learn object oriented programming where it started

I will use a search engine before I ask dumb questions.

Last edited by kgun; 08-13-2009 at 11:51 AM.
Reply With Quote