0

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');


?>
4
Contributors
4
Replies
8
Views
4 Years
Discussion Span
Last Post by pritaeas
0

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.

0

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?

0

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);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.