Php mssql database connection class instance

Source: Internet
Author: User
This article mainly introduces the mssql database connection class of php, and demonstrates various common operations of PHP for mssql databases in the form of a class instance, including database connection, addition, deletion, modification, query, and other operations.

This article mainly introduces the mssql database connection class of php, and demonstrates various common operations of PHP for mssql databases in the form of a class instance, including database connection, addition, deletion, modification, query, and other operations.

This article describes the php mssql database connection instance code and shares it with you for your reference.

The specific implementation code is as follows:

The Code is as follows:

Class DB_ SQL {
Var $ Host = "";
Var $ Database = "";
Var $ User = "";
Var $ Password = "";
Var $ Link_ID = 0;
Var $ Query_ID = 0;
Var $ Record = array ();
Var $ Row = 0;

Var $ Errno = 0;
Var $ Error = "";
Var $ Auto_Free = 0; # set this to 1 to automatically free results

Function DB_ SQL ($ query = ""){
$ This-> query ($ query );
}
Function connect (){
If (0 = $ this-> Link_ID ){
$ This-> Link_ID = mssql_connect ($ this-> Host, $ this-> User, $ this-> Password );
If (! $ This-> Link_ID)
$ This-> halt ("Link-ID = false, mssql_pconnect failed ");
Else
@ Mssql_select_db ($ this-> Database, $ this-> Link_ID );
}
}
Function free_result (){
Mssql_free_result ($ this-> Query_ID );
$ This-> Query_ID = 0;
}

Function query ($ Query_String)
{

/* No empty queries, please, since PHP4 chokes on them .*/
If ($ Query_String = "")
/* The empty query string is passed on from the constructor,
* When calling the class without a query, e.g. in situations
* Like these: '$ db = new DB_ SQL _Subclass ;'
*/
Return 0;
If (! $ This-> Link_ID)
$ This-> connect ();

# Printf ("
Debug: query = % s
", $ Query_String );

$ This-> Query_ID = mssql_query ($ Query_String, $ this-> Link_ID );
$ This-> Row = 0;
If (! $ This-> Query_ID ){
$ This-> Errno = 1;
$ This-> Error = "General Error (The MSSQL interface cannot return detailed error messages ).";
$ This-> halt ("Invalid SQL:". $ Query_String );
}
Return $ this-> Query_ID;
}

Function next_record (){

If ($ this-> Record = mssql_fetch_row ($ this-> Query_ID )){
// Add to Record [ ]
$ Count = mssql_num_fields ($ this-> Query_ID );
For ($ I = 0; $ I <$ count; $ I ++ ){
$ Fieldinfo = mssql_fetch_field ($ this-> Query_ID, $ I );
$ This-> Record [strtolower ($ fieldinfo-> name)] = $ this-> Record [$ I];
}
$ This-> Row + = 1;
$ Stat = 1;
} Else {
If ($ this-> Auto_Free ){
$ This-> free_result ();
}
$ Stat = 0;
}
Return $ stat;
}

Function seek ($ pos ){
Mssql_data_seek ($ this-> Query_ID, $ pos );
$ This-> Row = $ pos;
}
Function metadata ($ table ){
$ Count = 0;
$ Id = 0;
$ Res = array ();
$ This-> connect ();
$ Id = mssql_query ("select * from $ table", $ this-> Link_ID );
If (! $ Id ){
$ This-> Errno = 1;
$ This-> Error = "General Error (The MSSQL interface cannot return detailed error messages ).";
$ This-> halt ("Metadata query failed .");
}
$ Count = mssql_num_fields ($ id );

For ($ I = 0; $ I <$ count; $ I ++ ){
$ Info = mssql_fetch_field ($ id, $ I );
$ Res [$ I] ["table"] = $ table;
$ Res [$ I] ["name"] = $ info ["name"];
$ Res [$ I] ["len"] = $ info ["max_length"];
$ Res [$ I] ["flags"] = $ info ["numeric"];
}
$ This-> free_result ();
Return $ res;
}

Function affected_rows (){
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// Return mssql_affected_rows ($ this-> Query_ID );
$ RsRows = mssql_query ("Select @ rowcount as rows", $ this-> Link_ID );
If ($ rsRows ){
Return mssql_result ($ rsRows, 0, "rows ");
}
}

Function num_rows (){
Return mssql_num_rows ($ this-> Query_ID );
}

Function num_fields (){
Return mssql_num_fields ($ this-> Query_ID );
}
Function nf (){
Return $ this-> num_rows ();
}

Function np (){
Print $ this-> num_rows ();
}

Function f ($ Field_Name ){
Return $ this-> Record [strtolower ($ Field_Name)];
}

Function p ($ Field_Name ){
Print $ this-> f ($ Field_Name );
}

Function halt ($ msg ){
Printf (" Database error:% S
", $ Msg );
Printf (" MSSQL Error: % S (% s)
",
$ This-> Errno,
$ This-> Error );
Die ("Session halted .");
}
}

I hope this article will help you with PHP programming.

,

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.