Issue
I am currently unable to connect to my database using PDO in PHP.
Specs
- PHP version 5.6.9
- Database: Microsoft SQL Server 2014 on 2008 R2
- From phpinfo(): PDO drivers: mysql, odbc, sqlite
- Using IIS 6.1
What I've tried
I have just installed the drivers in the SQLSRV32.exe file from Microsoft (from this link: https://www.microsoft.com/en-us/download/details.aspx?id=20098) to my PHP extensions folder, however I still cannot seem to connect to my database. (And yes, I restarted IIS)
I can get the connection to work with older connection methods.
My Code
My current PHP code for PDO connection is as follows:
try {
$this->pdo = new PDO('odbc:host='.$this->hostname.':'.$this->port.';dbname='.$this->database,$this->username,$this->password);
} catch (PDOException $e) {
echo 'Failed to get DB handle: '.$e->getMessage().'<br/>';
exit;
}
The above code is inside my MyPDO class constructor so that when I instantiate my class, it is connected $pdo = new MyPDO();.
My Code's Result
The issue is that I am getting this error as output:
Failed to get DB handle: SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Any help resolving this issue is greatly appreciated!
new PDO('odbc:host='.$this->hostname.':'.$this->port.';dbname='.$this->database, $this->username, $this->password);new PDO("odbc:dsn_name", $user, $pass);