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.

<?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");
        }

        else
        {

            $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
    {
        mysql_close($this->myconn);

        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..

<?php

    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 />";

    $connection->closeConnection();
?>

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. It’s actually a cool and useful piece of information. I’m satisfied that
    you shared this useful information with us. Please stay us up to date like this.

    Thank you for sharing.

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

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

  4. 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

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

    $host=”localhost”;
    $username=”root”;
    $password=””;
    $dbname=”dbName”;

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

    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

    Thanks

Leave a reply to Nivetha Cancel reply

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