Php connection mssql database beginner php notes

Source: Internet
Author: User
Tags mysql functions php and mysql

Copy codeThe Code is as follows:
<? Php
$ ServerSite = ".";
$ Db = "phpdemo ";
$ Name = "sa ";
$ Pass = "sa ";
$ Conn = @ mssql_connect ($ serverSite, $ name, $ pass) or die ("database connection error! ");
@ Mssql_select_db ("phpdemo", $ conn );
Echo 'This can be use! ';
$ OK = @ mssql_query ("insert into test (name) values ('ossem ')", $ conn );
Echo 'this database is: '. $ conn;
If ($ OK)
{
Echo "OK ";
} Else
{
Echo "false ";
}
?>

If PHP and MySQL are connected, the link between PHP and MSSQL is actually very simple;
Local and remote connections of MSSQL are supported. Take the local link as an example:
MS SQLServer 2005 is installed on the machine;
Configure the system before connection:
1. Check the php5.2.5 \ ntwdblib. dll file. One of the following is under the default file and 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


4. Use the following method to connect to ms SQL Server 2005:
The Code is as follows:
Copy codeThe Code is as follows:
<? Php
// 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 ("<br/> ");
}
?>

We can see that these functions and MySQL functions are corresponding for easy use!

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: \ AppServ \ php5 \ ext ".
(4) If the machine still reports an error, c: \ AppServ \ php5 \ ext \ php_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: \ AppServ \ php5 \ ext \ php_mssql.dll; c: \ AppServ \ php5 \ ntwdblib. dll)
In addition, remember to restart the server after setting it.
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, modify the hosts file under \ system32 \ drivers \ etc of the machine where php is located, and add the name of the machine where the ip address of the machine where 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.

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.