# Connect to MSSQL by freetds fail



## cesjr (Feb 1, 2016)

Hi everyone
I installed Apache 2.4 and PHP 5.5 on FreeBSD 10.1, and I also install freetds to connect MS-SQL server. But  Apache error log still reporting 

```
mssql_query(): Unable to connect to server
```

Below is some pkg info of apache and php 5.5 

```
apache24-2.4.18                Version 2.4.x of Apache web server
mod_php5-5.4.45,1              PHP Scripting Language
php5-5.4.45                    PHP Scripting Language
php5-bz2-5.4.45                The bz2 shared extension for php
php5-ctype-5.4.45              The ctype shared extension for php
php5-dom-5.4.45                The dom shared extension for php
php5-extensions-1.7            "meta-port" to install PHP extensions
php5-filter-5.4.45             The filter shared extension for php
php5-gd-5.4.45_1               The gd shared extension for php
php5-gettext-5.4.45            The gettext shared extension for php
php5-hash-5.4.45               The hash shared extension for php
php5-iconv-5.4.45              The iconv shared extension for php
php5-json-5.4.45               The json shared extension for php
php5-mbstring-5.4.45_1         The mbstring shared extension for php
php5-mcrypt-5.4.45             The mcrypt shared extension for php
php5-mssql-5.4.45              The mssql shared extension for php
php5-mysql-5.4.45              The mysql shared extension for php
php5-mysqli-5.4.45             The mysqli shared extension for php
php5-openssl-5.4.45            The openssl shared extension for php
php5-pdo-5.4.45                The pdo shared extension for php
php5-pdo_sqlite-5.4.45         The pdo_sqlite shared extension for php
php5-phar-5.4.45               The phar shared extension for php
php5-posix-5.4.45              The posix shared extension for php
php5-session-5.4.45            The session shared extension for php
php5-simplexml-5.4.45          The simplexml shared extension for php
php5-sqlite3-5.4.45            The sqlite3 shared extension for php
php5-tokenizer-5.4.45          The tokenizer shared extension for php
php5-xml-5.4.45                The xml shared extension for php
php5-xmlreader-5.4.45          The xmlreader shared extension for php
php5-xmlwriter-5.4.45          The xmlwriter shared extension for php
php5-zlib-5.4.45               The zlib shared extension for php
```
I also have /usr/local/libexec/apache24/libphp5.so and include it in apache config file ==>

```
LoadModule php5_module        libexec/apache24/libphp5.so
```
The test.php is working fine 

```
<?php

// Show all information, defaults to INFO_ALL
phpinfo();

?>
```
The freetds.conf, and my php script

```
# A typical Microsoft server
[MSSQL]
        host = 192.168.1.9
        port = 1433
        tds version = 7.0
        client charset=UTF-8
```


```
<?
        $host="MSSQL";
        $usr="username";
        $pw="TEST";
        date_default_timezone_set("Asia/Taipei");
        mssql_connect($host,$usr,$pw) or DIE("DATABASE FAILED TO RESPOND.");

        mssql_select_db('globalweb');
?>
```
I also can using `tsql -S MSSQL -U username` to connect to MS-SQL server and running select language. But webserver still can't run my php page, apache error log still reporting 
	
	



```
mssql_query(): Unable to connect to server
```


I think I have to set php.ini and let PHP know how to connect MSSQL by freetds, but I don't know how to do. May I miss some steps? Any suggestion will be appreciated.


----------



## ab2k (Feb 2, 2016)

Hi, try to create a file file.php in shell and put this contents to it, after you fill the things up - try to run it from shell `php file.php`


```
<?php

$link = mssql_connect('192.168.1.9', 'username', 'TEST');

if (!$link || !mssql_select_db('globalweb', $link)) { die('Unable to connect or select database!'); }

$version = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($version);

echo $row[0];

mssql_free_result($version);

?>
```

This code will try to connect to MSSQL server, select your database and show MSSQL version. If everything of this works - then you have a problem with a query string in your code. If not - then maybe a problem with a firewall on MSSQL server. And please note that mssql_* things are deprected in PHP 7 and use of PDO is recommended.


----------



## cesjr (Feb 24, 2016)

OK,thanks ,I will try this.


----------

