Reputation: 561
After a lot of reading and searching I was able to get PDO DBLIB working in Centos 6.4 32bits (there is a lot of documentation for 64 bits and not for 32 bits). To my surprise the following code work from the shell command ($ PHP test.php )
<?php
try {
$conn = new PDO('dblib:host='.$host.':1433;dbname='.$db, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
But when visiting the file test.php from a web browser I get the following error:
ERROR: SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
I am also able to connect to SQL Servers from the shell using the command:
tsql -H 192.168.1.120 -p 1433 -U sa
What am I missing?
Upvotes: 1
Views: 1896
Reputation: 561
In my case SELinux (I am using CentOS) was blocking the connection when done from Apache web server in the linux box to the sql server in the windows box. To allow the connection you have two options:
Disable SELinux
As root or sudo run the two commands:
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
To check if SELinux is blocking your network and DB connections run this command:
getsebool -a | grep httpd_can_network_connect
If the results are:
httpd_can_network_connect --> on
httpd_can_network_connect_db --> on
You are good, if the are off then run the first commands in this answer.
Upvotes: 1