'connect mssql using php on Centos

I have installed freetds and I want to connect to mssql server via php. I am using centos.

root@server [~/php]# tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v0.91
             freetds.conf directory: /usr/local/etc
     MS db-lib source compatibility: no
        Sybase binary compatibility: no
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 5.0
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: no

The php code I use to connect to mssql server. (Its located in a subdirectory) I am able to connect to remote mssql server via local mssql server 2008.

<?php
// Older FreeTDS installations need the FREETDSCONF Environment variable
putenv('FREETDSCONF=../usr/local/etc/freetds.conf');
// Current release of FreeTDS uses the FREETDS environment variable. So we set both to be sure
putenv('FREETDS=../usr/local/etc/freetds.conf');
$mssql = mssql_connect('116.214.26.236' , 'dbname', 'password');
//mssql_select_db('myDb',$mssql);


if (!$mssql) {
    die('Something went wrong while connecting to MSSQL');
}else echo 'Success';

?>

My TDS config file

#   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory.  
#
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf".  

# Global settings are overridden by those in a database
# server specific section
[global]
        # TDS protocol version
;   tds version = 4.2

    # Whether to write a TDSDUMP file for diagnostic purposes
    # (setting this to /tmp is insecure on a multi-user system)
    dump file = /tmp/freetds.log
;   debug flags = 0xffff

    # Command and connection timeouts
;   timeout = 10
;   connect timeout = 10

    # If you get out-of-memory errors, it may mean that your client
    # is trying to allocate a huge buffer for a TEXT field.  
    # Try setting 'text size' to a more reasonable limit 
    text size = 64512

# A typical Sybase server
[egServer50]
    host = symachine.domain.com
    port = 5000
    tds version = 5.0

# A typical Microsoft server
[egServer70]
    host = 116.214.26.236
    port = 1433
    tds version = 7.0

The error that I get

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 116.214.26.236 in /home/butter/public_html/investonline/mssql.php on line 6 Something went wrong while connecting to MSSQL

Please guide me on where am I getting wrong.

I have searched on google some blogs says the below line value has to be yes, MS db-lib source compatibility: no

My php.ini conf file

mssql
MSSQL Support   enabled
Active Persistent Links 0
Active Links    0
Library version FreeTDS

Directive   Local Value Master Value
mssql.allow_persistent  On  On
mssql.batchsize 0   0
mssql.charset   no value    no value
mssql.compatability_mode    Off Off
mssql.connect_timeout   5   5
mssql.datetimeconvert   On  On
mssql.max_links Unlimited   Unlimited
mssql.max_persistent    Unlimited   Unlimited
mssql.max_procs Unlimited   Unlimited
mssql.min_error_severity    10  10
mssql.min_message_severity  10  10
mssql.secure_connection On  On
mssql.textlimit Server default  Server default
mssql.textsize  Server default  Server default
mssql.timeout   60  60

Also let me know how should I get the logs captured so that I can know at what step there is an error.

Also why am I not able to connect to mssql server.



Solution 1:[1]

You can use PDO php library to connect with sql server, you can use like this $output = new PDO ('mssql:host=SERVERHOST;dbname=DBNAME','DBUSERNAME','DBPASSWORD');

Solution 2:[2]

Got to this link and do as it says. Best to just use Microsoft's driver.

http://strangenut.com/blogs/dacrowlah/archive/2012/04/13/installing-and-using-the-microsoft-sql-server-odbc-driver-for-linux.aspx

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hitesh
Solution 2 Ross