How to connect PHP to SQL Server in Linux

Source: Internet
Author: User
Tags microsoft iis

Raise Questions

A few days ago, I made a very strange project. Our company developed a set of SP-side short message service software based on the SGIP Protocol of China Unicom to provide Unicom's 130 short message service. This system is in Windows 2000, and the database uses Microsoft SQLServer2000, and has been running normally for a while. Recently, to provide short message user information on the WEB, you need to read and write SQL Server databases from the WEB. The best partner of SQL Server databases should be Microsoft IIS ASP server scripts, however, my company has always believed that the stability and security of IIS + ASP are not satisfactory, and I hope to use PHP scripts to read and write SQLServer in Linux.

Analyze problems

There is no problem with the PHP script to read and write SQLServer. It works well in Apache for windows and Windows IIS. Generally, it can be connected through ODBC or SQLServer Client, which are all ready-made in Windows. However, ODBC and SQLServer Client are not available in Linux. We need to install them on our own.

Solve the problem

I. Related Software

Freetds Source:Ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/freetds-0.53.tgz

This software can use Linux and Unix to connect MS SQLServer and Sybase databases.

Ii. installation and configuration steps

Step 1: Compile and install freetds:

Get the 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 (extract)

Cd freetds-0.53 (enter the directory after decompression)

./Configure-prefix =/usr/local/freetds -- with-tdsver = 7.0

Gmake)

Gmake install (installation)

For more information about configure, -- prefix =/usr/local/freetds indicates that it is installed in the/usr/local/freetds directory, -- with-tdsver = 7.0 refers to the installation of the tds 7.0 version (the most open is that I did not add this compilation parameter, and the result is compiled as 5.0 by default. 5.0 the port connecting to the database is 4000, not 1433 of SQLServer)

Step 2: Re-compile PHP4

./Configure [-- with-apxs -- with-mysql...] -- with-sybase =/usr/local/freetds (sybase)

Make

Make install

Step 3: Configure freetds

Vi/usr/local/freetds/etc/freetds. conf

For detailed configuration, see the description in this file.

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 Windows domain login or SQLServer account login.

Step 4: configure the PHP. ini file

Find; extension = mssql70.so

Remove comments

Extension = mssql70.so

Step 5: Create 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. If you get a link number, congratulations, you have configured it. If the Call to undefined function: mssql_connect () appears () it indicates that you are not correct in the installation and configuration process.

Note: The sqlserver name is the host parameter defined in/usr/local/freetds/etc/freetds. conf. If you write an IP address, it is the IP address.

For other database operations, see related mssql functions.

Note: Chinese characters are not supported in SQL statements !!!

Step 6: Debug

If the connection fails, find it in the freetds configuration file; dump file =/tmp/freetds. log line, comment out the semicolon, and then execute the test script to check/tmp/freetds. log File, which can tell you a lot of error information to help you solve the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.