To connect PHP to MSSQL, the system needs to install MSSQL and PHP. In the configuration of PHP. ini
; Extension = php_mssql.dll; remove
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 );
Note: using PHP to operate MSSQL is easier than connecting to MySQL in ASP. Therefore, when MSSQL and MySQL are required to coexist, it is easier to use PHP to connect MSSQL to operate the coexistence of MySQL and MSSQL. if ASP is used to connect to MySQL, you need to install a MySQL driver. By default, ODBC in windows is not installed. Sorry...
1. Install at least MSSQL client on the Web Server
2. Open PHP. ini and remove the semicolon before extension = php_mssql.dll.
Required: extension_dir
3. We recommend that you use php <= 4.0.9 <= 5.0.3. Currently, I have not successfully connected PHP 4.010 or 5.0.3.
4. You can go to phpe.net to obtain the corresponding class on the database connection page.
The following is a class that I modified based on it.
<? PHP
/**
* MSSQL database connection class
**/
Class SQL {
VaR $ server;
VaR $ username;
VaR $ password;
VaR $ database;
VaR $ linkid = 0;
VaR $ queryresult;
VaR $ lastinsertid;
VaR $ pagenum = 0; // use for paging --- a total of several data records
VaR $ er;
/**
* Constructor
**/
Function SQL ($ Server = '', $ username ='', $ Password = '', $ database = ''){
$ This-> Server = $ server;
$ This-> username = $ username;
$ This-> Password = $ password;
$ This-> database = $ database;
}
/**
* Database connection
**/
Function db_connect (){
$ This-> linkid = mssql_pconnect ($ this-> server, $ this-> username, $ this-> password );
If (! $ This-> linkid ){
$ This-> ER = "db_connect ($ this-> server, $ this-> username, $ this-> password) error ";
Return 0;
}
If (! Mssql_select_db ($ this-> database, $ this-> linkid )){
$ This-> ER = "mssql_select_db ($ this-> database, $ this-> lastinsertid) error ";
Return 0;
}
Return $ this-> linkid;
}
/** Public
* Function: Check the database, if exist then select
* Exist: return 1
* Not exist: Return 0
*/
Function selectdatabase (){
If (mssql_select_db ($ this-> database ))
Return 1;
Else
Return 0;
}
/**
* Data Operations
**/
Function query ($ Str ){
If ($ this-> linkid = 0 ){
$ This-> ER = "the database is not connected !! ";
}
$ This-> queryresult = mssql_query ($ Str );
// $ This-> queryresult = mssql_query ($ STR, $ this-> linkid );
If (! $ This-> queryresult ){
$ This-> ER = "$ Str. No operation is successful. query Error !! ";
Return 0; // ***************** for PHP 4.3.9 or a later version of Error 1
}
Return $ this-> queryresult;
}
/**
* Data Acquisition
**/
Function fetch_array ($ result ){
If ($ result! = "") $ This-> queryresult = $ result;
$ Rec = mssql_fetch_array ($ this-> queryresult );
If (is_array ($ REC )){
Return $ REC;
}
// $ This-> ER = "No data is obtained! ";
Return 0;
}
/** Public
* Function: Free the query result
* Success return 1
* Failed: Return 0
*/
Function freeresult ($ result = ""){
If ($ result! = "") $ This-> queryresult = $ result;
Return mssql_free_result ($ this-> queryresult );
}
/**
* Obtain the number of affected rows.
* Get the number of rows that have been operated
**/
Function num_rows ($ result = ""){
If ($ result! = ""){
$ This-> queryresult = $ result;
$ ROW = mssql_num_rows ($ this-> queryresult );
Return $ row;
}
}
/**
* Retrieve query results --- multiple
**/
Function result_ar ($ STR = ''){
If (empty ($ Str )){
Return 0;
}
$ Back = array ();
$ This-> queryresult = $ this-> query ($ Str );
While ($ ROW = $ this-> fetch_array ($ this-> queryresult )){
$ Back [] = $ row;
}
Return $ back;
}
/**
* Database information Paging
* $ Result database operations
* STR = SQL statement
* Page = page number
* Shownum = display pages
*/
Function page ($ STR, $ page = 0, $ shownum = 5 ){
$ Back = array (); // return data
$ Maxnum = 0;
If ($ STR = ""){
$ This-> ER = "no data ";
Return 0;
}
$ This-> queryresult = $ this-> query ($ Str );
If ($ this-> queryresult ){
If ($ page = ""){
$ Nopa = 0;
} Else {
$ Nopa = ($ page-1) * $ shownum;
If ($ nopa <0 ){
$ Nopa = 0;
}
}
$ Maxnum = $ this-> num_rows ($ this-> queryresult );
$ K = 0;
$ I = 0;
$ Dd = $ this-> fetch_array ($ this-> queryresult );
While ($ DD & $ nopa <= $ maxnum & $ I <$ shownum ){
If ($ nopa >=$ maxnum) $ nopa = $ maxnum;
Mssql_data_seek ($ this-> queryresult, $ nopa );
$ ROW = $ this-> fetch_array ($ this-> queryresult );
$ Nopa ++;
$ I ++;
$ Back [] = $ row;
If ($ nopa >=$ maxnum ){
Break;
}
}
}
$ This-> pagenum = $ maxnum;
Return $ back;
}
/**
* Page number of the HTML page
*/
Function page_html ($ datanum = 0, $ page = 1, $ shownum = 3, $ web, $ post = ''){
If ($ datanum = 0 ){
$ Back = "no data to be queried ";
} Else {
If ($ shownum <= 0 ){
$ Shownum = 3;
}
If ($ page <= 0 ){
$ Page = 1;
}
If (empty ($ web )){
$ Web = "#";
}
$ Pagenum = Ceil ($ datanum/$ shownum );
If ($ page <= 1 ){
$ Top = "homepage <";
} Else {
$ Top = "<a href = '". $ Web ."? Page = 0 & ". $ Post." 'target = '_ Self'> homepage </a> ";
}
If ($ page! = 1 ){
$ Uppage = "<a href = '". $ Web ."? Page = ". ($ page-1)." & ". $ Post." 'target = '_ Self'> previous page </a> ";
} Else {
$ Uppage = "Previous Page ";
}
If ($ page <$ pagenum ){
$ Downpage = "<a href = '". $ Web ."? Page = ". ($ page + 1)." & ". $ Post." 'target = '_ Self'> next page </a> ";
} Else {
$ Downpage = "next page ";
}
If ($ page = $ pagenum ){
$ Foot = "> last page ";
} Else {
$ Foot = "<a href = '". $ Web ."? Page = ". $ pagenum." & ". $ Post." 'target = '_ Self' >>> last page </a> ";
}
$ Back = <EOT
Total $ pagenum page & nbsp;
Page $ page/$ pagenum page $ top & nbsp; $ uppage & nbsp; $ downpage & nbsp; $ foot
EOT;
}
Return $ back;
}
} // End class
?>