PDO DBLIB for accessing SQL Server 2008 and 2012

After a lot of reading and searching, I was able to get a PDO DBLIB working on Centos 6.4 32bits (there is a lot of documentation for 64 bits, not 32 bits). To my surprise, the following code works from a 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 test.php file from a web browser, I get the following error:

ERROR: SQLSTATE [HY000] Unable to connect: Adaptive Server is not available or does not exist (Severity 9)

I can also connect to SQL Server from the shell using the command:

tsql -H 192.168.1.120 -p 1433 -U sa

      

What am I missing?

+1


source to share


1 answer


In my case SELinux (I am using CentOS) was blocking the connection when it was made from Apache webserver in linux box to sql server in box. To allow the connection, you have two options:

  • Disable SELinux

  • As root or sudo run two commands:

setsebool -P httpd_can_network_connect 1

setsebool -P httpd_can_network_connect_db 1

To check if SELinux is blocking network and DB connections, run the following 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 fine if they are off then run the first commands in this answer.

+1


source







All Articles