Mssql_connect function in php cannot be used by default. We need to configure it as follows:
In PHP. ini, open the extension = php_mssql.dll option (that is, remove the previous; number)
2. Download the correct version of ntwdblib. dll (2000.80.194.0)
3. Overwrite c: windowssystem32ntwdblib. dll
4. Overwrite phpntwdblib. dll (if this step is not completed successfully, put the php5ts. dll on which PHP depends under the system32 directory)
Use mssqlserver to connect
1. Connect to MSSQL
The code is as follows: |
Copy code |
$ 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
The code is as follows: |
Copy code |
Mssql_select_db ("dbname "); |
3. Execute the query
The code is as follows: |
Copy code |
$ 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.
The code is as follows: |
Copy code |
Mssql_query ("update tbname set username = 'niunv' where id = 1 ");
|
4. Get the number of record sets
The code is as follows: |
Copy code |
Echo mssql_num_rows ($ rs ); |
5. Obtain the record set
The code is as follows: |
Copy code |
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.
It is the ID of the last new record.
The code is as follows: |
Copy code |
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
The code is as follows: |
Copy code |
Mssql_free_result ($ rs ); |
8. Close the connection
The code is as follows: |
Copy code |
Mssql_close ($ conn ); |
SQL SERVER instance 1 connected to ODBC
The code is as follows: |
Copy code |
<? $ Connection = odbc_connect ("mydata", "userid", "passwd "); $ Query = "select * from tab_1 where no> 0 "; $ Result = odbc_do ($ connection, $ query ); PRint "<table border = '1' width = '000000' id = 'tab1' cellPadding = '1' cellSpacing = '0' align = 'top' bordercolorlight = '#8080' bordercolordark = '#008000'> "; While (odbc_fetch_into ($ result, & $ fields )) { Print "<tr> n "; For ($ I = 21; $ I <= 31; $ I ++ ){ Print "<td width = '6% '> <input style = 'background-COLOR: # ffffff; BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: # ffffff 1px solid; BORDER-RIGHT: # ffffff 1px solid; BORDER-TOP: # ffffff 1px solid; BORDER-BOTTOM: # ffffff 1px solid; COLOR: #000000; FONT-SIZE: 9pt 'size = '7' type = 'text' name = text $ I value = $ temp> "; } } Print "</table> "; Odbc_close ($ connection ); ?> |