简体   繁体   中英

PHP Connect to MSSQL From Both Linux and Windows

I am using a linux build server ( http://circleci.com ) for auto builds and testing; however i need to connect to a MSSQL server. I can develop on windows fine and connect using the MS php SQLSVR drivers, but i cannot connect on the Linux build server as the drivers are windows only.

My Question is this: is there a single way to connect to MSSQL server via php for both windows and linux? tried odbc but then you need bdlib and FreeTDS on linux - this would constitute a code change thus meaning the tests are not 100%, for example on windows environment it would use odbc:{SQL Server} and on linux it would be odbc:FreeTDS

It just doesn't seem right to have a check to see what system is being used and then choose the corresponding db connection string.

I am using PHP 5.5.1

No, there is not a single way however it would be trivial to do something like this:

if ('Linux' === PHP_OS){
    $pdo = new PDO("dblib:dbname=$database_name;host=$database_server", $username, $password);
}else{
    // $pdo = whatever you're using on your Windows box now.
}

You will need to set up your odbc.ini, odbcinst.ini and freetds.conf files on the Linux server as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM