PHP connection MSSQL database beginner PHP notes

Source: Internet
Author: User
Tags mysql functions

CopyCode The 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:
1 error:
fatal error: Call to undefined function mssql_connect ()
solution:
Use MSSQL _ series functions
to use these two functions, you need to use them in PHP. INI settings:
(1) Allow DCOM, you need to set PHP. com. remove the Semicolon ";" before allow_dcom = true.
(2) to use MSSQL extension, remove the semicolon before extension = php_mssql.dll in PHP. ini. (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.
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)
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. Otherwise, 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.

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.