How to write a Connection class to MySQL database using PHP

Hi friends to day i am going to tell you about something about PHP and MySQL. We all know that PHP is a one of best scripting language for web development.We can build awesome dynamic web pages using PHP.But we must build something valuable other than produce unimportant web sites. So today i am going to tel you how to write and test the OOP based connection class to MySQL database using PHP.


 * @author damith
 * @copyright 2011

class createConnection //create a class for make connection
    var $host="localhost";
    var $username="username";    // specify the sever details for mysql
    Var $password="password";
    var $database="database name";
    var $myconn;

    function connectToDatabase() // create a function for connect database

        $conn= mysql_connect($this->host,$this->username,$this->password);

        if(!$conn)// testing the connection
            die ("Cannot connect to the database");


            $this->myconn = $conn;

            echo "Connection established";


        return $this->myconn;


    function selectDatabase() // selecting the database.
        mysql_select_db($this->database);  //use php inbuild functions for select database

        if(mysql_error()) // if error occured display the error message

            echo "Cannot find the database ".$this->database;

         echo "Database selected..";       

    function closeConnection() // close the connection

        echo "Connection closed";



OK.. Now you have create the connection class and now you can call this class inside your project. To test the connection i will get another PHP file and create a object from my existing  connection class.. see below code how i am going to do it..


    include ('connection.php');

    $connection = new createConnection(); //i created a new object

    $connection->connectToDatabase(); // connected to the database

    echo "<br />"; // putting a html break

    $connection->selectDatabase();// closed connection

    echo "<br />";


I think you know how to test this.Start wamp server and goto your local host’s folder and test it.. If you success you can see the below message on a browser

Now you have connected to the MySQL friends.. see you in next post

37 thoughts on “How to write a Connection class to MySQL database using PHP

  1. Very well article. This is very helpful to a newbie like me. 🙂 Keep it up 🙂

    Can I ask some question, How can I insert/update/delete using this method? Can you give me some example related to this article?

    Thanks in advance

  2. sir ? for selectDatabase() function it doesn’t require Sql Connection ?

    My Code is the same for you but there is no Database Selected ?

  3. this is same with mySqli

    class createConnection
    var $host = “localhost”;
    var $username = “root”;
    var $password = “”;
    var $database = “hostnile”;
    var $myConn;

    function connectToDatabase()
    $db = new mysqli($this->host, $this->username, $this->password, $this->database);
    if (mysqli_connect_errno()) {
    printf(“connect failed : %s\n”, mysqli_connect_errno());
    } else {
    $this->myConn = $db;
    return $this->myConn;

    and my include file

    $connection = new createConnection(); //i created a new object
    $connection->connectToDatabase(); // connected to the database

  4. I have a question why I should use all this, usually I do this


    $db=new mysqli($host, $username, $password, $dbname);
    printf(“connect failed : %s\n”, mysqli_connect_errno());

    and I include this file config.php in my index file and I have connection to database ready and running normally why I should use class and all this things here


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.