Php SQL server operation class PHP MSSQL

Source: Internet
Author: User

<? PHP
Abstract class Dao {
Protected static $ _ conn; // single-State connection
Protected $ _ error; // error message
Protected $ _ errno; // error code
Protected $ _ perpagerecord; // Several data entries per page
Protected $ _ currenttime;
Protected $ _ recordcount;

Public Function _ construct (){
}

// Search for a record by ID
Public Function findbyid ($ table, $ id ){
$ SQL = "select * from {$ table} Where id = {$ id }";
Return $ this-> uniquequery ($ SQL );
}

// Connect to the database
Private function _ connect (){
If (! Is_resource (Self ::$ _ conn )){
SELF: $ _ conn = mysql_connect (
Localhost,
Mysql_user,
Mysql_pass
);
Mysql_select_db (mysql_database_name, self: $ _ conn );
Mysql_query ("set names". mysql_code, self: $ _ conn );
}
}

// Check and execute the query
Private function _ check_query ($ result, $ SQL ){
If (! $ Result ){
$ This-> _ error = mysql_error ();
$ This-> _ errno = mysql_errno ();
$ This-> _ error ("invalid SQL:". $ SQL );
}
}

// Set the number of entries displayed on each page (used for paging. This value is useless when the query offset is empty.
Public Function setperpagerecord ($ perpagerecord ){
$ This-> _ perpagerecord = $ perpagerecord;
}

// Execute the SQL statement and return the result
Protected function _ sendsql ($ SQL, $ offset = false ){
$ This-> _ connect ();
If (is_numeric ($ offset) & is_numeric ($ this-> _ perpagerecord )){
$ SQL = $ SQL. "Limit {$ offset},". $ this-> _ perpagerecord;
}

$ Result = mysql_query ($ SQL, self: $ _ conn );
$ This-> _ check_query ($ result, $ SQL );
Return $ result;
}

// Obtain multiple Datasets
Public Function query ($ SQL, $ offset = false ){
$ Result = $ this-> _ sendsql ($ SQL, $ offset );
$ Datas = array ();
While ($ ROW = mysql_fetch_array ($ result )){
$ Datas [] = $ row;
}
Return $ datas;
}


// Obtain the total number of records, if not paged
Public Function getrecordcount ($ SQL ){
$ Result = $ this-> _ sendsql ($ SQL );
Return $ this-> _ recordcount = mysql_num_rows ($ result );
}

// Obtain the total number of pages
Public Function gettotalpage (){

}

// Obtain a unique record
Public Function uniquequery ($ SQL ){
$ Result = $ this-> _ sendsql ($ SQL );
Return mysql_fetch_array ($ result );
}

// Obtain multiple values (used when selecting a single field)
Public Function fetchvalues ($ SQL, $ offset = false ){
$ Result = $ this-> _ sendsql ($ SQL, $ offset );
$ Datas = array ();
While ($ ROW = mysql_fetch_array ($ result )){
$ Datas [] = $ row [0];
}
Return $ datas;
}

// Handle errors
Private function _ error ($ MSG ){
Printf ("</TD> </tr> </table> <B> database error: </B> % S <br>/N", $ MSG );
Printf ("<B> MySQL error </B>: % s (% s) <br>/N ",
$ This-> _ errno,
$ This-> _ error );
Die ("session halted .");

}

// Obtain a value
Public Function fetchvalue ($ SQL ){
$ Result = $ this-> _ sendsql ($ SQL );
$ Value = mysql_fetch_row ($ result );
Return $ value [0];
}


// Execute non-query statements
Public Function execute ($ SQL ){
$ This-> _ sendsql ($ SQL );
Return mysql_insert_id (SELF: $ _ conn );
}

// Execute the delete statement. return must be returned; otherwise, the correct result cannot be obtained.
Public Function deletesql ($ SQL ){
Return $ this-> _ sendsql ($ SQL );
}

// Close the database
Public Function close (){
Mysql_close (Self ::$ _ conn );
}


// Destructor
Public Function _ destruct (){
@ $ This-> close ();
}

}
?>

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.