Home > Article > Backend Development > How to connect MS SQLServer with PHP under Linux_PHP Tutorial
Ask a question
I did a very strange project a few days ago. Our company developed a set of SP-side short message service software based on China Unicom's SGIP protocol. China Unicom 130 SMS service is provided. This system is under Windows 2000. The database uses Microsoft SQLServer2000 and has been running normally for some time. Recently, in order to provide some information of short message users on the WEB, it is necessary to read and write the SQL Server database from the WEB. Originally, the best partner of the SQL Server database should be Microsoft IIS ASP server script, but our company has always believed that IIS+ASP The stability and security are not satisfactory. I hope to use PHP scripts to read and write SQL Server under Linux.
Analysis of the problem
Originally, there is no problem with PHP script reading and writing SQL Server. It can work very well under Apache for windows and Windows IIS. Generally You can connect through ODBC or SQLServer Client, which are ready-made under Windows. However, there are no ready-made ODBC and SQLServer Client under Linux, and we need to install them ourselves.
Solve the problem
1. Related software
freetds Source: ftp://ftp.ibiblio.org/pub/Linux/ ALPHA/freetds/freetds-0.53.tgz
This software can connect MS SQLServer and Sybase databases using Linux and Unix.
2. Installation and configuration steps
Step 1: Compile and install freetds:
After getting freetds-0.53.tgz
cp freetds-0.53.tgz /tmp/. (Copy the freetds package to the /tmp directory)
cd /tmp (enter the directory)
tar zxvf freetds-0.53.tgz (unzip)
cd freetds-0.53 (enter the unzipped directory)
./configure –prefix=/usr/local/freetds --with-tdsver=7.0
gmake (generate Makefile, I have tested it, make can also)
gmake install (installation)
About the above I want to talk about configure, --prefix=/usr/local/freetds refers to installing into the directory /usr/local/freetds, --with-tdsver=7.0 refers to installing tds 7.0 version (I did not add it in the end) With this compilation parameter, the result is compiled to 5.0 by default. The port for 5.0 to connect to the database is 4000, not 1433 of SQL Server)
Step 2: Recompile PHP4
./configure [--with-apxs - -with-mysql...] --with-sybase=/usr/local/freetds (please note it is sybase)
make
make install
Step 3: Configure freetds
vi /usr /local/freetds/etc/freetds.conf
See the instructions in this file for specific configuration
Example: (Typical configuration)
[sqlserver]
host = sql_server_name_or_host_ip (your SQLServer machine name or IP Address)
port = 1433
tds version = 7.0
In this configuration file, you can configure two methods of Windows domain login or SQLServer account login
Step 4: Configure the php.ini file
Find; extension=mssql70.so
Remove the comment; to
extension=mssql70.so
Step 5: Establish a database connection in php
$link=mssql_connect( "sqlserver",$your_username,$your_password) or die ("can't Connect to Database");
echo $link;
Run the above script in the browser. Congratulations if you get a link number. You have already configured it. If Call to undefined function: mssql_connect() appears, it means that you should carefully read the above installation and configuration process to see which step you are wrong.
Note: The sqlserver name is the host parameter defined in /usr/local/freetds/etc/freetds.conf. If you write the IP address, it is the IP address.
Reference related mssql functions for other database operations
Note that Chinese is not supported in sql statements!!!
Step 6: Debugging
If you cannot connect, please configure it in the freetds configuration file Find the line;dump file = /tmp/freetds.log, comment out the preceding semicolon, execute the test script again, and check the /tmp/freetds.log file. It can tell you a lot of error information to help you troubleshoot the problem.