Configure PHP to support SQL Server database under Ubuntu system

Source: Internet
Author: User

Recently in a project, the database for the project was Microsoft's SQL Server, the database was installed on a different Windows servers, and the project was deployed on Ubuntu server. So how do you link SQL Server to your project on Linux? Here I use FreeTDS to do the middleware of the link database, the following is my practice steps:

1. Download the latest FreeTDS, visit http://www.freetds.org/, or use wget on Ubuntu ftp://ftp.freetds.org/pub/freetds/stable/ Freetds-stable.tgz download a stable version.

2. Installing the FreeTDS and configuration FreeTDS

CD into the directory where the FreeTDS is located,

$ tar zxvf freetds-stable.tgz (unzip)
$./configure--prefix=/usr/local/freetds--with-tdsver=8.0--enable-msdblib

$ make
$ make Install

Configure steps and make steps can be done with the privileges of the normal user, the install step is best with the root user, or the /usr/local/ directory has read and write permissions to the user.

Configuration:

Edit/etc/ld.so.conf, inserting a row in it:
/usr/local/freetds/lib
Then run the following command to make the changes effective:
Ldconfig

Edit FreeTDS configuration file, configure database connection information

Vim/usr/local/freetds/etc/freetds.conf as follows:

Locate the EGSERVER70 node here, configure the server address where your data resides, and do not change the general port unless the database is accessed using a different port. Note that in general, there is no client charset option in this configuration file.

Need to be added manually to prevent database garbled. This is set to UTF8, which is encoded in accordance with the database code. Save the exit after the modification is complete. Then verify that the configuration is OK.

cd/usr/local/freetds/bin/

./tsql-h your SQL Server addr-p 1433-u user name-p password-d database

Note that your password has special characters such as! and # Sort of, then need to add the escape character \!\#, so it will be recognized by FreeTDS.

If the command is executed, the following information is returned

Unexpected EOF from the server

That means the FreeTDS version information is incorrect, the information about the FreeTDS version can be consulted http://www.freetds.org/userguide/choosingtdsprotocol.htm

Then the corresponding version information modification only need to modify the TDS version of a particular database connection in /usr/local/freetds/etc/freetds.conf , here I am connected to SQL Server 2005, the default version is 7.0 , but the connection error, so here I modified into 7.1 OK, as follows:

Once the modification is complete, test again if the result is returned, then the connection to the database is successful.

3.php Connecting SQL Server

The above test directly with FreeTDS test database can link database, then how to let PHP also support SQL Server? The developer mode of PHP5 is needed here, and I have only installed PHP5 by default.

So here you need to install Php5-dev, directly with the command Apt-get install Php5-dev, after the installation is completed in the/usr/bin directory will see PHPIZE5 and other related PHP development of this mode plug-in

Here I installed the default is php5.5.9, so need to download php5.5.9 source code, and then go to ext/mssql/Compile support for SQL Server. Source:http://us2.php.net/get/php-5.5.9.tar.xz/from/this/mirror

It can also be downloaded with wget. After downloading with the tar XF php-5.5.9.tar.gz, go to the Ext/mssql directory to execute the following command:

/usr/bin/phpize5

./configure--with-php-config=/usr/local/webserver/php/bin/php-config5--with-mssql=/usr/local/webserver/freetds /
Make && make install

After the installation is successful, you can have mssql.so in the/usr/lib/php5/20121212 directory, of course, the general directory of this file is located in/usr/lib/php5, as for the date of the year after the composition of this folder, on different servers are not the same.

Next, edit the php.ini configuration file/etc/php5/apache2/php.ini

Add extension=mssql.so to the file

Then restart the Apache service apache2 restart

Create a new index.php file under the/var/www/html directory, cat index.php

Echo Phpinfo ();

Then visit localhost/index.php and you can see the support of MSSQL

This means that PHP has support SQL Server database, if you want to test the data can be removed, then you can write the demo code, new test.php

Header("content-type:text/html; Charset=utf-8 "); $msdb=mssql_connect ("mssql.yourdomain.com:1433", "username", "password"); if(!$msdb) {          Echo"Connect SQL Server Error"; Exit; } mssql_select_db ("database_name",$msdb); $result= Mssql_query ("SELECT Top 5 * from table",$msdb);  while($row= Mssql_fetch_array ($result)) {          Var_dump($row); } mssql_free_result ($result);

All the steps here have been completed.

Configure PHP to support SQL Server database under Ubuntu system

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.