PHP naturally has good support for MySQL, but if you want to use PHP to operate SQL Server, it will take some time. Today it happened that a project in the team needed to use PHP to operate SQL Server, so I helped configure the environment.
First of all, the system version of the server is SUSE Linux Enterprise Server 10 SP3.
1. Install FreeTDS
Address: FreeTDS
2. Configure FreeTDS and connect test
The configuration file of FreeTDS is placed in etc in the installation directory. According to the configure parameters in the first step, our FreeTDS is installed in /usr/local/freetds:
Since I don’t know the specific configurable items of FreeTDS, I won’t go into details here, but I will provide a more important configuration to solve the problem of Chinese garbled characters. Add the following statement in the configuration file:
Then, we use the tsql command to test whether we can connect to the SQL Server database normally:
If the connection is normal, the following statement should be displayed:
3. Install the mssql extension for php
The PHP version on the server is 5.3.13, PHP has been installed under /usr/local/services/php, and the extension directory is /usr/local/services/php/extensions. Here’s how to install the mssql extension:
make #Generate expansion files and place them in the module folder of the current directory
cp modules/mssql.so /usr/local/services/php/extensions/ #Copy the extension file to the PHP extension directory
4. Configure php.ini and verify the installation results
Open php.ini and add the following expansion statement:
After restarting the PHP service (the server uses php-fpm), print phpinfo. If the following configuration appears, it means that PHP can operate SQL Server normally.
1. Maybe you don’t need to install it. You can solve this problem by installing unixODBC yourself.
2. Maybe there are Linux security restrictions. Change the
SELINUX=enforce
in /etc/selinux to :SELINUX=disabled
3. Maybe mssql restrictions prevent remote login
Some modules also need to recompile PHP. It’s not as simple as just modifying php.ini