php5.3.x Connecting Ms SQL server2008

Source: Internet
Author: User
Tags sql server driver php error phpinfo management studio sql server management sql server management studio

Opening

The database required by the virtual travel site must use MS SQL Server because of the Bishi needs of the teacher.

The Web programming language I'm best at is PHP, but linking MS SQL Server in PHP is a hassle, and I personally analyze the cause of this: because the big advantage of PHP is that it's free, but MS SQL Server, while the graphical interface is simple to operate, But its expensive licensing fees are prohibitive, and with MySQL's impressive performance and strong community support, there are fewer people using PHP + MS SQL Server in a real enterprise environment.

In fact, throughout the online article, the reason someone chose such a combination (PHP + MS SQL Server) is due to the legacy of the system. PHP programmers, for example, require two of development on an enterprise website, or the MS SQL Server database that the enterprise originally used. In short, after 12 hours of fighting, and finally communication success, now the process and experience recorded as follows.

1. Installing MS SQL server2008 Express Database

The installation process is relatively straightforward. But it's important to remember that we set the--or our default--the database instance name at the time of installation.

1.1 Default instance name of the database

In the case of Ms SQL Server2008 Express, the typical default instance name is SQLEXPRESS. And I'm setting up SQL Server.

1.2 Setting the database login mode

We must set a password for the SA user so that we can write programs to access the database in the future. The password I personally set is "6 1".

2. Log in to the database

Use administrative Tools: SQL Server Management Studio to log into the database for management. 1.

Figure 1 Logging into Ms SQL server2008 database

2.1 Problems you will encounter at the time of landing

I ran into a problem when I logged in, 2.

Figure 2 issues encountered when logging in

The error message is as follows:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or could not be accessed. Verify that the instance name is correct, and that SQL Server is configured to allow remote connections. (provider: Named pipe provider, ERROR:40-Unable to open connection to SQL Server) (Microsoft SQL Server, Error: $)

2.2 Problem Solution

After I have some solutions in Baidu, I learned that the server name cannot be filled in by default: (local). It should be filled in in this format: computer name \ instance name. Take me personally, for example, 3.

Figure 3 Correct login fill in

Since you can log in to the database, it means that our installation is not a problem. Let's do some of the settings for PHP.

3 Prepare the appropriate library file for PHP

After PHP5.3, the native code of its connected database is maintained by the Microsoft team. The library file we need (the. dll file under Windows) is not in PHP's source code package, so we need to download it separately.

Referring to the official PHP documentation is the best option.

http://php.net/manual/zh/book.sqlsrv.php

3.1 Download and install Microsoft SQL Server Native Client

To say a digression, this step is when I write a blog, read PHP manual to know. No wonder I first time in the operation, encountered a PHP error, the Chinese effect is "this machine is not installed Microsoft SQL Server Native Client." English is good, can read the Official document is kingly!

Download the Microsoft SQL Server Native client URL as follows:

http://php.net/manual/zh/sqlsrv.requirements.php

3.2 Download the appropriate Microsoft SQL Server Driver for PHP

By official documentation we can be informed that the applicable program corresponding to php5.3.x is SQLSRV 3.0. The download URL is as follows:

http://www.microsoft.com/en-us/download/details.aspx?id=20098

3.3 Select the appropriate library file

According to the official documentation, we--SQLSRV30 the program downloaded in 3.2 steps. After the exe--is decompressed, 8 drive files are obtained (the driver file ends with a. dll). 4.

Figure 4 SQLSRV30. EXE decompression after expansion

According to the official documentation, the php5.3 thread-safe version should choose between Php_pdo_sqlsrv_53_ts.dll and Php_sqlsrv_53_ts.dll two files, placed under the Ext folder in the PHP root directory. For example, in my machine, the path is D:\lamp\php5\ext.

3.4 How to know if your PHP is thread safe

In Phpinfo, we look at a piece of information like this. 5.

Figure 5 Phpinfo Output Information

"Enable" means that PHP is a thread-safe version and is non-thread safe.

4. Configuring the php.ini File

For the configuration of the php.ini file, you can refer to the following URL completely.

http://msdn.microsoft.com/en-us/library/cc296203 (v=sql.105). aspx

4.1 Making PHP Load sqlsrv drivers

In the php.ini extension Module loading area-file extension that block location, fill in the following configuration, so that php default load sqlsrv driver.

Extension=php_sqlsrv_53_ts.dll
4.2 Making PHP Load PDO_SQLSRV drivers

In the same position as in 4.1, fill in the following configuration.

Extension=php_pdo_sqlsrv_53_ts.dll

To enable PHP to turn on the PDO driver, we also have to turn on the PDO driver.

Extension=php_pdo.dll
4.3 Verifying that the pdo_sqlsrv is loaded successfully

See the word Yellow Line in phpinfo, indicating that the load was successful. As shown in 6.

Figure 6 Output of the Phpinfo

5. Dynamic Loading at PHP runtime

I also now know that PHP has such a magical feature, let us temporarily comment out the PHP configuration file for you just write the configuration, try the dynamic loading function.

According to the official documentation, the DL () function was abolished in php5.3. I was therefore unable to test.

But Microsoft's official PHP code is as follows:

DL (' Php_pdo_sqlsrv_53_ts.dll ');
6. Precautions

In MS SQL Server, the name of the data table can not be called user, because there is a system table name is user!

php5.3.x Connecting Ms SQL server2008

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.