4

So I am writting some code so I can access a Microsft SQL Server. The code I am writting is in a Centos 7 machine. I've installed the php mysql and mssql packages but when I run

echo "<pre>", print_r(PDO::getAvailableDrivers()), "</pre>";

I get the following output:

Array
(
   [0] => mysql
   [1] => pgsql
   [2] => sqlite
)

I've restarted the lampp and still got the same result.

I've also edited php.ini to include extension=php_mssql.so, and restarted lampp and still got the same output.

When I run the pdo script:

try
{
    $con = new PDO("dblib:host=".$db_addr.";dbname=".$db_name.";charset=utf8", '".$db_usr."', '".$db_pass."');
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    echo "ERROR: ". $e->getMessage();
    die(); 
}

I always get:

ERROR: could not find driver

I relalise that this needs to be configured in the php.ini, but the the drivers that are output are commented out of php.ini, hence I am quite confused.

Can someone guide me in the proper direction?

EDIT: I've followed @jap1968 answer in here, but I got stuck at where I am now.

Community
  • 1
  • 1
Comum
  • 453
  • 5
  • 21
  • Maybe http://stackoverflow.com/questions/21315354/pdo-dblib-on-centos-6-x ? – VolkerK Feb 10 '15 at 12:08
  • I've followed that answer and got to where I am. I'll edit my question to include this as well. – Comum Feb 10 '15 at 12:10
  • Did the `yum install php-mssql` pull other dependencies? Do you see at least the `mssql` extension after restarting the http server in the output of `phpinfo()`? – VolkerK Feb 10 '15 at 12:11
  • yes, it did, and got no errors, freetds, libzip, php.common. php-pdo, unixODBC. I also see the extension in the output of `phpinfo()`. – Comum Feb 10 '15 at 12:16
  • also, i've tried to access the db using `tsql` from the console and managed to have access. – Comum Feb 10 '15 at 12:19
  • "Ask" `echo get_cfg_var('cfg_file_path');` whether you've edited the right .ini file. (Isn't there a fedora tool to set/unset the httpd modules and php modules?) – VolkerK Feb 10 '15 at 12:21
  • `/opt/lampp/etc/php.ini` and this is the .ini I am editing. I don't know about that tool. This is the first time I need to had a driver to php, so I am noobish in this area. – Comum Feb 10 '15 at 12:25

1 Answers1

1

I've found why this didn't work. I installed my lamp stack using xampp. So I had trouble getting the extra modules.

To fix it I reinstaled an apache server (httpd), mysql (MariaDB) and php and I then managed to add the driver I needed, following @jap1968's answer in here.

After the driver is installed CentOS only enables MS SQL Server connections using the console, as default. So when I accessed my script using the browser, I got an Error message saying the server was unavailable.

To have access through the browser you need to use (I ran this with root permissions, also it may take some seconds):

# setsebool -P httpd_can_network_connect=1
Community
  • 1
  • 1
Comum
  • 453
  • 5
  • 21