Linux under PHP to connect MS SQL Server approach

Source: Internet
Author: User
Tags ftp iis connect odbc php script sybase microsoft iis linux
Server|sqlserver

Ask a question

A few days ago did a very strange project, my company developed a set of China Unicom SGIP Protocol of the SP terminal Short Message Service software, to provide unicom 130 SMS services. This system is under Windows 2000, the database is using Microsoft SQLServer2000, and has been running for some time. Recently, because to provide some information on the web for short message users, you need to read and write the SQL Server database from the Web, the best partner for SQL Server database should be Microsoft IIS ASP Server script, but our company always think iis+ ASP's stability and security are not satisfactory, I hope to be able to read and write SQL Server with PHP script under Linux.

Analyze Problems

PHP script read-write SQL Server is not a problem, in the Apache for Windows and Windows IIS can work well, can generally be connected through ODBC or SQL Server client, which is available under Windows. But there are no out-of-the-box ODBC and SQL Server Client under Linux, and we need to install them ourselves.

solve the problem

first, related software

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

The software is able to connect MS SQL Server and Sybase databases with Linux and UNIX.

Second, installation configuration steps

The first step: Compile and install FreeTDS:

After getting freetds-0.53.tgz

CP freetds-0.53.tgz/tmp/. (Copy FreeTDS package to/tmp directory)

Cd/tmp (Enter directory)

Tar zxvf freetds-0.53.tgz (decompression)

CD freetds-0.53 (into the unpacked directory)

./configure–prefix=/usr/local/freetds--with-tdsver=7.0

Gmake (Generate makefile, I tried, make is OK)

Gmake Install (Installation)

About the above configure I would like to say that--prefix=/usr/local/freetds refers to the installation to/usr/local/freetds this directory,--with-tdsver=7.0 refers to the installation of TDS 7.0 Version (most open is I did not add this compilation parameter, the result is compiled to 5.0 by default.) 5.0 the port that connects the database is 4000, not SQL Server 1433)

Step Two: Recompile PHP4

./configure [--with-apxs--with-mysql ...]--with-sybase=/usr/local/freetds (please note that Sybase)

Make

Make install

Step Three: Configure FreeTDS

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

Specific configuration See the description in the file

Example: (Typical configuration)

[SQL Server]

Host = SQL_SERVER_NAME_OR_HOST_IP (your SQL Server machine name or IP address)

Port = 1433

TDS Version = 7.0

In this configuration file you can configure Windows domain login or SQL Server account login two ways

Step Fourth: Configure the php.ini file

Extension=mssql70.so to find;

To remove a note from;

Extension=mssql70.so

Fifth step: Establish a database connection in PHP

$link =mssql_connect ("SQL Server", $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 so congratulations, you have been configured, if the call to undefined Function:mssql_connect () that a careful look at the above installation configuration process to see if you are not right.

Note: SQL Server name is the host parameter defined in/usr/local/freetds/etc/freetds.conf, if you write an IP address that is an IP address.

Other database operations reference related MSSQL functions

Note that Chinese is not supported in SQL statements!!!

Sixth Step: Debugging

If you cannot connect, find it in the FreeTDS configuration file, dump file =/tmp/freetds.log This line, comment out the preceding semicolon, and then execute the test script to see/tmp/ Freetds.log file, it can tell you a lot of wrong information to help you eliminate the problem.



Related Article

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.