0

Hi

I am trying to connection my database ODBC using function.

I am getting an error Connection error: could not find driver

mycode is

<?php
class Database{

    // specify your own database credentials
    private $host = "servername";
    private $db_name = "databsename.schemaname";
    private $username = "username";
    private $password = "password";
    public $conn;

    // get the database connection
    public function getConnection(){

        $this->conn = null;

        try{
            $this->conn = new PDO("odbc:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>

Please help me to fix the issue.

3
Contributors
12
Replies
34
Views
2 Years
Discussion Span
Last Post by rpv_sen
Featured Replies
  • **Here is the working code** <?php class Database{ // specify your own database credentials private $host = "odbc:Driver={SQL Server};server=servername;DATABASE=databasename"; private $username = "username"; private $password = "password"; public $conn; // get the database connection public function getConnection(){ $this->conn = null; try{ $this->conn = new PDO($this->host, $this->username, $this->password); }catch(PDOException $exception){ echo … Read More

0

if i connect this by odbc_connect its working. but when i use PDO i am getting that error

0

For the benefit of others out there, could you please show the solution?

1

Here is the working code

<?php
class Database{

    // specify your own database credentials
    private $host = "odbc:Driver={SQL Server};server=servername;DATABASE=databasename";
    private $username = "username";
    private $password = "password";
    public $conn;

    // get the database connection
    public function getConnection(){

        $this->conn = null;

        try{
            $this->conn = new PDO($this->host, $this->username, $this->password);
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>
This question has already been answered. 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.