Summary of Some Problems and Methods for connecting php to the mssql database

Source: Internet
Author: User
Tags mssql php and zip

 

Environment:
-Apache 2.2.6
-PHP 5.2.5
-SQL Server 2005
-Windows XP SP2

Steps:

1. Configure PHP5 to connect to ms SQL Server as usual
2. Download the correct version of ntwdblib. dll (2000.80.194.0). Note that this version number does not work for this version of dll. It has been a long time since it started to discover this problem. Remember...

Address: http://www.webzila.com/dll/1/ntwdblib.zip
3. Overwrite apache2.2.6binntwdblib. dll
4. Overwrite php5.2.5ntwdblib. dll
5. Run SQL Server Configuration Manager: SQL Server Configuration Manager to enable Protocols.
6. Allow named pipelines "named pipes" and "TCP/IP"
7. Right-click "TCP/IP" to open the Properties tag "ip addresses"
8. Enter 1433 in the TCP Dynamic port "TCP Dynamic Ports".
9. Restart SQL Server, Apache, and PHP
Connect to ms SQL Server 2005 using the following methods:
Mssql_connect ('localhost, 100', USERNAME, PASSWORD );

 
Bytes -----------------------------------------------------------------------------------------------

Configure php

1. Open php. in and remove the annotation symbol of extension = php_mssql.dll.

2. Open php. in and change mssql. secure_connection = Off to on.

3. Copy php_mssql.dll to the directory specified by extension_dir in php. in or the system32 directory of the system. (Php_mssql.dll is included in the php compression installation package ).

After completing the preceding steps, restart apache.

Note: in actual use, if php is manually installed under iis through the php compressed file, the machine must be restarted, not just iis.

4. Ensure that ntwdblib. dll is available in php or system32, and check the attribute to make sure that the version is 8.00.194 instead of 7. 0 ......

Other settings

If php apache SQL Server2000 is on the same machine, the access is basically normal.

If the SQL Server and php machines are separated, make sure that the machine name on which the SQL Server server is located can be pinged. If the machine name cannot be pinged, modify the hosts file under system32driversetc of the machine where php is located, add the name of the machine where the ip address of the machine where the SQL server is located.

If the connection is still inaccessible, check whether the machine on which php is located is converted to mdac. Or simply install the SQL Server client.

For php connection to sql2005, I found a lot of information on the network in my csdn blog. At, I finally got it done.

Php connection to sql2005 is now integrated, and FAQ is integrated.

 

Configure the system before connection:

1. Check that the php5.2.5ntwdblib. dll file contains one by default, which cannot be connected and replaced.

Download the correct version of ntwdblib. dll (2000.80.194.0), Address: http://webzila.com/dll/1/ntwdblib.zip

2. Configure php

A. Open php. in and remove the annotation symbol of extension = php_mssql.dll.

B. Open php. in and change mssql. secure_connection = Off to on.

C. Copy php_mssql.dll to the directory specified by extension_dir in php. in or the system32 directory of the system. (Php_mssql.dll is included in the php compressed installation package ).

After completing the preceding steps, restart apache.

Note: in actual use, if php is manually installed under iis through the php compressed file, the machine must be restarted, not just iis.

3. Configure sqlserver
A. Run SQL Server Configuration Manager: SQL Server Configuration Manager to enable Protocols.
B. Allow named pipelines "named pipes" and "TCP/IP"
C. Right-click "TCP/IP" and open the Properties tag "ip addresses"
D. Enter 1433 in the TCP Dynamic port "TCP Dynamic Ports".
E. Restart SQL Server

Bytes -----------------------------------------------------------------------------------------------
4. Use the following method to connect to ms SQL Server 2005:
The code is as follows:


// Link to the database
$ Conn = mssql_connect ('localhost', 'sa ', '123 ');
Mssql_select_db ('Gu _ dde ', $ conn );
// Query statement
$ Query = "select * from dde_top ";
$ AdminResult = mssql_query ($ Query );
// Output result
$ Num = mssql_num_rows ($ AdminResult );
For ($ I = 0; $ I <$ Num; $ I ++)
   {
$ Row = mssql_fetch_array ($ AdminResult );
Echo ($ Row [1]);
Echo ("");
   }  
?>


Enter http: // 127.0.0.1/

-----------------------------------------------------------------------

5. FAQ FAQs:

1 error:

Fatal error: Call to undefined function mssql_connect ()

Solution:

Use MSSQL _ series functions
To use these two methods, you need to set them in php. ini:
(1) to allow DCOM, remove the semicolon ";" before com. allow_dcom = TRUE in php. ini.
(2) to use the MSSQL extension, remove the semicolon ";" in php. ini before extension = php_mssql.dll. (Key)
(3) confirm that extension_dir is the correct path. Take the local machine as an example: extension_dir = "c: AppServ5.2.6phpext ".
(4) If the machine still reports an error, c: AppServ5.2.6phpextphp _ mssql. dll cannot be found, but this file exists clearly.
Solution: copy php_mssql.dll and ntwdblib. dll to the system directory system32 and restart the test ..
(Note: The above two dll files are not in the same directory. My files are c: AppServ5.2.6phpextphp _ mssql. dll; c: AppServ5.2.6phpntwdblib. dll)
In addition, remember to restart the server after setting it.

2. mssql_connect () Unable to connect to server

Check whether the SQLServer2005 server is normal. Check whether TCP/IP is enabled.

Right-click to view properties at the same time:

Enabled? Select yes

After confirming that the server is correct, check whether the ntwdblib. dll file is stored in c: windowssystem32.

At the same time, ensure that the version of the ntwdblib. dll file corresponds to that of sqlserver:

The following is the correspondence:

2. If the ntwdblib. dll version is 2000.2.8.0, it corresponds to SqlServer2000 (this is the network query information and speculation, and 2000 is not installed)

2. The ntwdblib. dll version 2000.80.194.0 corresponds to SqlServer2005 (this is an experiment that can be used. I installed 2005 in my notebook)

3. The ntwdblib. dll version 2000.80.2039 corresponds to SqlServer2008 (this is a guess that 2008 is not installed)

6. Other problems:

If php apache SQL Server2000 is on the same machine, the access is basically normal.

If the SQL Server and php machines are separated, make sure that the machine name on which the SQL Server server is located can be pinged. If the machine name cannot be pinged, modify the hosts file under system32driversetc of the machine where php is located, add the name of the machine where the ip address of the machine where the SQL server is located.

If the connection is still inaccessible, check whether the machine on which php is located is converted to mdac. Or simply install the SQL Server client.

The solution is as follows:

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

If php_mssql.dll is not copied to c: windowssystem32

Pay attention to the version of the ntwdblib2093.dll file, otherwise it will be very depressing.

If your MSSQL database is 2000, the operations are basically the same.

Add a server in the win2003 server system. In the past, other servers used php5.0/5.1, and the database was SQL Server 2000. This time, we plan to install SQL Server 2005 and php5.3.2. It is understood that php5.3 uses Microsoft's FastCGI mode, compared with the traditional CGI mode, this mode adopts a more reasonable startup mode for parsing processes, which is twice faster than CGI. The ISAPI mode is not supported in PHP5.3 or later versions.

 

 


Warning: mssql_connect (): Unable to connect to server:

What if apache + php is used in linux? It is impossible to install mssql.

The following is an article.

Php configuration:
Set the following settings in the php. Ini file and find
; Extension = php_mssql.dll remove the semicolon
Find extension_dir = d: extension
Your php. ini may not contain d: extension.
Change the path of php_mssql.dll under the extensions directory under the php installation directory. If you haven't moved it to another place (assume that your php installation path is d: php)
Change to extension_dir = d: phpextensions.
Then restart the web server.


This is easy to do, but it cannot be connected after such a setting. The error message is as follows:

Ms SQL Server database connection error! Check whether the database host variable settings are correct !!!

I checked the host's variable settings over and over again, but those settings were none of the problem at all. I searched the webpage and found the following clues:


Php.com documents:

I am trying to connect to SQL Server 2000 from PHP
I bumped to following warning:
Warning: mssql_connect (): Unable to connect to server: SERVERPortal
... On line 5

On line 5 there is:
$ Db_connect = mssql_connect ('serverportal ', 'sa', 'My _ passwd ');

I did the following
1. enabled php_mssql.dll extension in PHP. ini
2. every dll is in proper place (System32 or PHP folder), including ntwdblib. dll

I search lots of profile throught web, but no one give me proper answer to resolve it.

After a few hour, I found the problem was caused
Ntwdblib. dll, which version is 7.00.839, when I replaced old ntwdblib. dll with the new

Ntwdblib. dll, which version is 8.00.194, all problem are solved.

We had some, read a lot, of problems with MSSQL under Windows 2003.
We had 2 the same windows, php, php-ini, everything machines but only one cocould connect.

Unable to connect was the error message.

Finnaly we checked the version of ntwdblib. dll and the one distributed with PHP was 7. 00 ....

And the version of the one on the SQL Server install was 8. 00... so we copied this one in

The php and apache dir and it worked.

The problem was found in this way, and the problem was caused by ntwdblib. dll.

Ntwdblib. dll is mainly used to provide the SQL server connection service.

My php version is 4.3.9. In the windows/system32/directory where the server is installed, I found ntwdblib. the dll file version is 2000.2.8.0. This version supports SQL server 7.0 because all the files in the dlls will overwrite the system when installing PHP.

Directory, so when I use it to connect to SQL server 2000, it will of course be unable to connect.

Later, I found ntwdblib on a server where SQL server 2000 is installed normally. the dll version is 2000.80.2039.0. I copied the file to overwrite the previous version. After the server is restarted, everything works normally.


Add some simple php mssql operations to connect to the database.


1. Connect to MSSQL
$ Conn = mssql_connect ("instance name or server IP address", "username", "password ");

// Test the connection
If ($ conn)
       {
Echo "connection successful ";
       }

2. Select the database to connect
Mssql_select_db ("dbname ");

3. Execute the query
$ Rs = mssql_query ("select top 1 id, username from tbname", $ conn );
Or directly execute the update, insert, and other statements without assigning values to the returned results.
Mssql_query ("update tbname set username = 'niunv' where id = 1 ");

4. Get the number of record sets
Echo mssql_num_rows ($ rs );

5. Obtain the record set
If ($ row = mssql_fetch_array ($ rs ))
       {
$ Id = $ row [0]; // obtain the ID field value
$ Username = $ row [1]; // obtain the value of the username field.
       }

6. Get the ID of the new record
Set the id field to the IDENTITY Field. After the insert statement is executed, a @ IDENTITY global variable value is generated. The queried result is the ID of the last new record.
Mssql_query ("insert into tbname (username) values ('nv ')", $ conn );
$ Rs = mssql_query ("select @ IDENTITY as id", $ conn );
If ($ row = mssql_fetch_array ($ rs ))
       {
Echo $ row [0];
       }

7. Release record set
Mssql_free_result ($ rs );

8. Close the connection
Mssql_close ($ conn );

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.