iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Programming Programmers can seek advice or just show off their creations inside this forum.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-13-2009, 11:10 AM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,709
kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9
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.

Last edited by kgun; 08-13-2009 at 12:51 PM.
Reply With Quote
  #2 (permalink)  
Old 08-14-2009, 08:31 AM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,709
kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9
Default Re: A walk in the garden part 1.

Quote:
Originally Posted by kgun View Post
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.
This will hopefully make the above statements more clear. Inheritance is indicated by the extends keyword.

PHP example code for illustrative purposes that will not function:

PHP Code:
<?php
/**
* NOTE:  Dont try to run this code.  It will not work, since it needs implementation etc.
*
* Object interfaces allow you to create code which specifies which methods
* a class must implement, without having to define how these methods are handled. 
* Interfaces are pure templates.  
* An interface can can only define functionality, but never implement it.
* All methods declared in an interface must be public, this is the nature of an interface.
* FYI, interfaces can define constructors, destructors, and magic methods. 
* This can be very helpful especially in the case of constructors when instantiating an 
* implementing class via reflection in some sort of factory. Of course, it is not 
* recommended to do such a thing since it goes against the nature of a true interface.
* Interfaces support multiple inheritance
* Source: http://www.php.net/manual/en/language.oop5.interfaces.php 
*/

interface ElectricalDevice{
  public function 
power_on();
  public function 
power_off();
}

interface 
FrequencyTuner{
  public function 
get_frequencey();
  public function 
set_frequency($f);
}

class 
ElectricFan implements ElectricalDevice{
  
// define ElectricalDevice...
}

class 
MicrowaveOven implements ElectricalDevice{
  
// define ElectricalDevice...
}

class 
StereoReceiver implements ElectricalDeviceFrequencyTuner{
  
// define ElectricalDevice...
  // define FrequencyTuner...
}

class 
CellPhone implements ElectricalDeviceFrequencyTuner{
  
// define ElectricalDevice...
  // define FrequencyTuner...
}

/**
* Abstract classes
* It is not allowed to create an instance of a class that has been defined as abstract.
* Any class that contains at least one abstract method must also be abstract.
* Methods defined as abstract simply declare the method's signature they cannot define the implementation. 
* Source:  http://www.php.net/manual/en/language.oop5.abstract.php
*/

abstract class AbstractClass
{
    
// Force Extending class to define this method
    
abstract protected function getValue();
    abstract protected function 
prefixValue($prefix);

    
// Common method
    
public function printOut() {
        print 
$this->getValue() . "\n";
    }
}

class 
ConcreteClass1 extends AbstractClass
{
    protected function 
getValue() {
        return 
"ConcreteClass1";
    }

    public function 
prefixValue($prefix) {
        return 
"{$prefix}ConcreteClass1";
    }
}

class 
ConcreteClass2 extends AbstractClass
{
    public function 
getValue() {
        return 
"ConcreteClass2";
    }

    public function 
prefixValue($prefix) {
        return 
"{$prefix}ConcreteClass2";
    }
}

$class1 = new ConcreteClass1;
$class1->printOut();
echo 
$class1->prefixValue('FOO_') ."\n";

$class2 = new ConcreteClass2;
$class2->printOut();
echo 
$class2->prefixValue('FOO_') ."\n";

//  Subclassing and interfaces.
class Person {
  
// Properties for Person objects.
}

interface 
IStudent {
  
// ...
}

interface 
IEmployee {
  
// ...
}

class 
Student implements IStudent extends Person {
  
// ...
}

class 
StudentEmployee implements IStudentIEmployee extends Person {
  
// ...

?>
Why is it useful to define an interface and implement it in a class? The answer lies in type. An implementing class takes on the type of the class it extends and the interface it implements. So to know an objects type is to know its capabilities. An interface joins types that are otherwise unrelated.

Links:
  1. PHP: Object Interfaces - Manual
  2. PHP: Class Abstraction - Manual
  3. PHP: Classes and Objects (PHP 5) - Manual
"Object return value problems are well known in php 5.*. PHP does not enforce a unified return value. There is no return class hinting, so there is nothing to prevent us from returning an error flag in stead of the promised object or primitive. When we do this, we have to rely on the client coder to test for the return type every time our error-prone method is called. This can be risky business. Trust no one".

Source: Matt Zandstra "PHP 5 Objects, Patterns, and Practice" chapter 4.

Example:

PHP Object return value

As far as I know this problem will be solved in PHP 6 that also introduces the important concept of name spaces to avoid class collisions (in different libraries).

Last edited by kgun; 08-14-2009 at 08:59 AM.
Reply With Quote
  #3 (permalink)  
Old 08-16-2009, 06:48 AM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,709
kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9
Default Re: A walk in the garden part 1.

For those of you that want to use the pear MDB2 database abstraction layer

Install pear:

  1. Manual :: Installation (PEAR) (Note it is possible to install a local PEAR copy on a shared host). There may be some information in these old posts if you need additional information:

    - PEAR for Beginners // Archives // Addicted To New by John Nunemaker

    - Application Development: Install PEAR on a shared Web host (Be selective when you read, but the article has general information of value)

    - http://forums.hostmysite.com/about6509.html (Skim the article)

    - PEAR on Shared Host - Codewalkers (Enough explanation for you?)
  2. User Guide: Manual :: User Guide
  3. Getting started: Manual :: Getting started
Package Information: MDB2:
  1. MDB2
  2. Search query: pear mdb2 tutorial
Additional information:

Scroll down to the pear section of the last site in my signature.

Note: You can manipulate .htaccess with File_HtAccess

I will not promise to write more regarding database connection etc. You should have enough information above.

Related WPW thread: Is PHP a powerful tool?

Last edited by kgun; 08-16-2009 at 08:00 AM.
Reply With Quote
  #4 (permalink)  
Old 08-16-2009, 09:53 AM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,709
kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9
Default Re: A walk in the garden part 1.

I could not edit my last post. Here
  1. Manual :: What is PEAR?
  2. Accounts
  3. An overview of pear packages
is two important links. Note the following sections in the first document:
  • Structured Libraries and Applications of PHP Code

    "Packages are distributed as gzipped tar files with a description file inside, and installed on your local system using the PEAR installer".
  • Code Distribution and Package Maintenance
  • pears relation to The PHP Extension Community Library (PECL)

Last edited by kgun; 08-16-2009 at 10:14 AM.
Reply With Quote
  #5 (permalink)  
Old 08-17-2009, 06:07 AM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,709
kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9
Default Re: A walk in the garden part 1.

Not a single comment or reaction to this important subject.

Quote:
Originally Posted by kgun View Post
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)
No need to reinvent the wheel. That task is already solved for you if you have a php version with the PDO extension. Look at the drivers page:

PHP: PDO Drivers - Manual

to find out which database drivers that are supported. If you have php 5.0, you can download the extension here: PECL :: The PHP Extension Community Library

Finally, if you decide to use pear, you may register on the pear forum and vote here:

PEAR Forum - Forum for PHP Extension and Application Repository

It is a never ending poll, so hopefully the installation of pear, at least on a shared host will become more user friendly in the future.

Note:
  1. Your hoster may not support Mysqli or PDO.
  2. Later versions of php 5.+ are by default compiled with central pear packages. So unless compiled with the

    -without-pear

    configuration flag

    you can use some packages directly.
Conclusion:
  1. Use the method in post #1 - #2 if you want to make your own database solution or the other options are not available because you are on a shared host etc.
  2. If you don't want to write SQL, use the pear class library and its database solution.
  3. If you want a general php object oriented database solution that functions on many platforms, use PDO.
  4. If you sign up with a new hosting company, ask about which database solutions are available. This has made me a lot of frustration since I use three different hosters with different versions of PHP installed. One hoster only supports PDO with MySQLITE. PHP MySQL PDO and MySQLi is not supported. But their PHP version 5.2.4 is fortunately compiled with pear that may be the best solution if you only need php and MySQL:

    include_path :/usr/local/share/pear (local and master value)

Last edited by kgun; 08-17-2009 at 08:21 AM.
Reply With Quote
  #6 (permalink)  
Old 08-18-2009, 06:13 AM
kgun's Avatar
WebProWorld 1,000+ Club
WebProWorld MVP
 
Join Date: May 2005
Location: Norway
Posts: 5,709
kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9kgun RepRank 9
Default Re: A walk in the garden part 1.

More on installing pear on a shared host: Installing pear on a shared host. - SitePoint Forums

Note The following two important cites from Matt Zandstra:

"PHP 5 Objects, Patterns, and Practice" chapter 14. "An introduction to pear."

Page 310 in my version:

"The DB package is worth mastering. It provides a platform-agnostic interface to multiple databases. The idea is that you write code to work with the DB API, and it handles the database specific syntax.

..............

This is an excellent example of good object-oriented design - an abstract class DB_COMMON defines an interface that supports the operations we have illustrated (in particular query() and nextIndex()), and a set of children provide the implementation. In this way your application is ready to work with MySQL, SQLite, MSSQL or Oracle with no change in your code (as long as you use standard SQL syntax).
"

So it is worth learning pear for many different reason.
Reply With Quote
Reply

  WebProWorld > Computer Assistance > Programming

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you "Walk the Walk"? box4blox Services for Sale/Hire 1 05-20-2007 09:24 PM
NeuStep Walk-Through Technology rbennett Marketing Strategies Discussion Forum 2 02-20-2006 02:02 PM
CSS Zen Garden Entry Narasinha Submit Your Site For Review 1 03-27-2005 07:11 PM
web designers who take pay then walk away...solutions? defcom2 Search Engine Optimization Forum 4 03-02-2005 10:22 AM
Home & Garden bmwindows Marketing Strategies Discussion Forum 2 02-13-2004 10:39 AM


All times are GMT -4. The time now is 08:23 PM.



Search Engine Optimization by vBSEO 3.3.0