QuoCAtted Text Here
Actually I'm passing connection Parameters by calling class Connection.Here what i cant understood is what is the way for passing the value in $link(which can declare as protected).when i want to use this connect class in other page how can i use this class.

<?php
class Connection
{

    protected $link;
    private $server, $username, $password, $db;

    public function __construct($server, $username, $password, $db)
    {
        $this->server = $server;
        $this->username = $username;
        $this->password = $password;
        $this->db = $db;
        $this->connect();
    }

    private function connect()
    {
        $this->link = mysql_connect($this->server, $this->username, $this->password);
    $result= mysql_select_db($this->db, $this->link) or die('sql error');

    }

    public function __sleep()
    {
        return array('server', 'username', 'password', 'db');
    }

    public function __wakeup()
    {
        $this->connect();
    }
}

$link= new Connection('localhost','root','11222','1251');


?>

The connect function lacks the connection functionality and lacks a returns statement. You should add a connect function mysql_connect which returns a valid link (or an error):

 private function connect()
{
    $this->link = mysql_connect($this->server, $this->username, $this->password);
    $result= mysql_select_db($this->db, $this->link) or die('sql error');
    return mysql_connect() or die('Could not connect: ' . mysql_error());
}

And a suggestion: it is recommended to use mysqli extension which is newer and better supported than mysql, which is going to be dropped at some stage.

I am not sure if methods __sleep() and __wakeup() have any usability here. You are making a connection in the constructor already so why have another method to do it again? And what is the purpose of the __sleep() method?

Line 35 should be changed.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

@broj1: http://php.net/__sleep is for serialization, which I doubt he's using.

commented: Tnx, I was not aware of that. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.