PHP Connection sql server 2005 Environment Configuration and Problem Solving

  • 2021-07-10 19:08:49
  • OfStack

1. Connect PHP under Windows to SQLServer 2005

Setting: Installed Windows operating system (Win7 or XP can be used, other systems have not been tested yet), under C disk; The relevant file for PHP is under c:/PHP, and its configuration file php. ini is under C:\ Windows.

Configure the system before connection:

1. Check the file php\ ntwdblib. dll. There is one under the installation directory of PHP by default, which cannot be connected and replaced.

Download the correct version of ntwdblib. dll (2000.80.194.0)

2. Configure php

a, open php. in and remove the comment symbol of extension=php_mssql. dll.

b, open php. in Change mssql.secure_connection = Off to on.

c, copy php_mssql. dll to the directory specified by extension_dir in php. in or under the system system32 directory, located at/PHP/ext.

apache needs to be restarted after the above steps are completed.

Note: In actual use, it is found that if php is manually installed under iis through php zip file, the machine must be restarted instead of just iis.

3. Configure sqlserver

a. Run SQL Server Configuration Manager: SQL Server Configuration Manager, open protocol Protocols

b. Allow named pipes "named pipes" and "tcp/ip"

c. Right-click "tcp/ip" to open the attribute Properties tab "IP Address"

d. Fill in 1433 on the TCP dynamic port "TCP Dynamic Ports" and pay attention to assigning the correct IP address.

e. Restart SQL Server

4. Connect MS SQL Server 2005 using the following:

The code is as follows, save as test. php:

< ?php

//Link database

$conn=mssql_connect('localhost','sa','123456');

mssql_select_db('gu_dde',$conn);

//query statement

$Query="select * from dde_top";

$AdminResult=mssql_query($Query);

//Output results

$Num=mssql_num_rows($AdminResult);

for($i=0;$i

Enter http://127.0.0.1/test. php

Make a visit:

5. FAQ Frequently Asked Questions:

1 Error reporting:

Fatal error: Call to undefined function mssql_connect()

Resolve:

Use the MSSQL_ family of functions

To use both, you need to configure it in php. ini:

(1) To allow DCOM, it is necessary to change the value of php. ini; com. allow_dcom=semicolon before TRUE "; "Remove.

(2) To use the MSSQL extension, php. ini; extension=php_mssql. Semicolon before dll "; "Remove. (Key)

(3) Confirm that extension_dir is the correct path. Take this machine as an example: extension_dir = "c:\ php\ ext".

(4) If the machine still reports that c cannot be found:\ php\ ext\ php_mssql. dll exists.

Solution: Copy php_mssql. dll, ntwdblib. dll to the system directory\ system32 to restart the test. .

(Note: The above two dll files are not in the same directory, mine is c:\ php\ ext\ php_mssql. dll; c:\ php\ ntwdblib. dll)

In addition, remember to restart the server after setting it up.

Already enabled. Choose Yes

After confirming that the server is correct, confirm that the ntwdblib. dll file location is placed under c:\ windows\ system32

At the same time, make sure that the version of ntwdblib. dll corresponds to the version of sqlserver:

The following is the correspondence:

1. ntwdblib. dll version is 2000.2.8. 0, which corresponds to SqlServer 2000 (this is a network search data and guess, not installed 2000)

2. ntwdblib. dll version 2000.80. 194.0 is corresponding to SqlServer 2005 (this is proved to be usable by experiments, I installed 2005 with a notebook)

3. ntwdblib. dll version 2000.80. 2039 corresponds to SqlServer 2008 (this is a guess that 2008 is not installed)

6. Other issues:

If php apache Sql Server 2005 are all on the same machine, access is almost no problem.

If Sql Server2005 and php machines are separated, it is necessary to confirm that the machine name of the machine where ping sqlserver is located can pass. If it does not pass, modify hosts file under\ system32\ drivers\ etc of the machine where php is located, and add one line of machine name of the machine where ip sqlserver is located.

If it is still inaccessible, it is necessary to confirm whether the machine where php is located has dimmed mdac. Or simply install the client of sqlserver under 1.

Solve the following problems:

1. Download the two files php_mssql. dll and ntwdblib. dll

php_mssql. dll This is easy to appear if this is not copied under c:\ windows\ system32

ntwdblib2093. dll This file should pay attention to the version, otherwise it will be very depressed later.

2. Connect php under Linux to sql server 2005

1. First, install other components in the LAMP environment except php as needed. Install freetds using freetds connection MS SQL in Linux

./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld --enable-shared --enable-static

make

make install

Important note 1: What should be noted in this step is-with-tdsver

2. Configure freetds

server2005 corresponds to tds version is 7.2 (it seems that everything that can be found online is written 8.0)

Configuration/usr/local/freetds/etc/freetds. conf

# A typical Microsoft server

[1.1.1.21]

host = 1.1.1.21

port = 1433

tds version = 7.0

Important note: When calling the mssql_connect function, it is important to note that its first argument should be [1.1. 1.21] (that is, configured in freetds. conf), not IP of sql server!

3. Compile and install php

The configuration items for compiling php are set as needed, but the configuration items that must be added are: --with-mssql=/usr/local/freetds/

Note: If the freetds directory cannot be found when compiling php and loading freetds, the following actions are required (official Faq):

touch /usr/local/freetds/include/tds.h

touch /usr/local/freetds/lib/libtds.a

4. After installing php, all the preparation work is completed. Next, you can restart apache and call sql server 2005 with php.


Related articles: