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


?>

Dani AI

Generated

— the pattern you started (an OO wrapper that holds a connection) is fine, but the implementation and some design choices in the thread need updating.

The old ext/mysql functions used in your example are deprecated and were removed from PHP (ext/mysql functions no longer exist in PHP 7+). New code should use either MySQLi or PDO; both are maintained and support prepared statements and other modern features. (php.net)

A practical, modern approach:

  • Keep credentials outside the class (config file or environment variables).
  • Build a small connection class that constructs a PDO instance (or accept a PDO passed in). Use PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION and prepared statements for safety. Inject the PDO into classes that need it rather than making everyone call a global Connection class — this makes testing and reuse much easier. Example (conceptual) below shows the minimal pattern to create and expose a PDO object:
class Database {
    private $pdo;
    public function __construct(string $dsn, string $user, string $pass, array $opts = []) {
        $default = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION];
        $this->pdo = new \PDO($dsn, $user, $pass, $default + $opts);
    }
    public function getPdo(): \PDO { return $this->pdo; }
}

Using PDO also gives clear controls for connection lifetimes and persistence; closing is simply unsetting the PDO variable. If you need persistent connections, read the PDO connection guidance and warnings first (temporary tables, transactions and server limits can cause surprises). (php.net)

About your use of sleep/wakeup: those magic methods are for serialization (sessions, caches). You normally should not serialize live connection resources — store credentials or DSN and re-connect on wake-up instead. (php.net)

Credit to for pointing away from ext/mysql and to for flagging __sleep. Avoid copying raw connection code into many files; centralize, inject, and rely on PDO/mysqli for current PHP versions.

Recommended Answers

All 4 Replies

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

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