Hello,
I'm using below PDO connection class
but didn't shows any result , Pls reply if u found any bugs..-

//name connect.class.php
class Connection  {

	private $connection;
	private $username;
	private $password;
	private $dsn;

	/**
	 * To connect to another database, change the "mysql" in the dsn attribute to
	 * the database you want. 
	 * for example: pgsql:dbname...
	 */
	public function __construct() {
		$this->dsn = "mysql:host=localhost;dbname=example";
		$this->username = "root";
		$this->password = "root";
	}

	public function setConnection($conn) {
		$this->connection = $conn;
	}

	public function getConnection() {
		return $this->connection;
	}
public function connect() {
		
			$pdoConnect = new PDO($this->dsn, $this->username, $this->password);
			$this->connection = $pdoConnect;
			
			}
}

//connection class call in index.php
<?php 
include("connect.class.php");

$conn= new Connection();
$db=$conn->connect();
if (!$db)
{ echo "Fail"; }
else {echo "Connect";}


?>

I haven't used PDO classes. But maybe a "return" in the connect function will do the trick ?

public function connect() {
$pdoConnect = new PDO($this->dsn, $this->username, $this->password);
return $this->connection = $pdoConnect;
}

thanks for quick reply, but after add 'return' in function problem remains the same.

You can give print_r(objectname) to check what values does these variables hold and debug !

Do you have a database on the local server called 'example'? And is there a user called 'root' with a password 'root' that has access to that database? Try testing this by connecting to the example database with that username/password in mysql and see if it works that way. Your connect() method looks correct.

Another thing you can try is to put the connect method in a try/catch block.

public function connect()
{
  $dsn = ...
  $username = ...
  $password = ...
  try
  {
    $this->connection = new PDO($this->dsn, $this->username, $this->password);
  }
  catch (PSOException $e)
  {
    echo 'Connection failed: ' . $e->getMessage();
  }
}

Yes , when i used below code then it Connects to database but in class it doesn;t work...

<?php

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'root';

/*** mysql password ***/
$password = 'root';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=example", $username, $password);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database';
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

Actually, I have just realised the mistake. You want the following code for your connect function in the Connection class:

public function connect()
{
   $dsn = ...
   $username = ...
   $password = ...
   try
   {
     $this->connection = new PDO($dsn, $username, $password);
   }
   catch (PSOException $e)
   {
   echo 'Connection failed: ' . $e->getMessage();
   }
}

Notice the removal of '$this' in the parameters to the constructor of the PDO object. That is because we want to reference the local variables as opposed to the class variables (which this signifies).

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.