Instance database connection class-PHP source code

Source: Internet
Author: User
Ec (2); require_once (& quot; Config. inc. php & quot;); system configuration file & nbsp; * & nbsp; $ DBHOST & quot; localhost & quot; Host Name & nbsp; $ DBUSER & quot; root & quot; script ec (2); script

Require_once ("Config. inc. php"); // System Configuration File

/*
$ DBHOST = "localhost"; // host name
$ DBUSER = "root"; // database username
$ DBPWD = ""; // Password
$ DBNAME = "test"; // Database Name
*/
// Define the Database Class

Class DataBase
{
// Define attributes

Var $ mConnId; // connection ID

Var $ mSqlString; // SQL statement to be executed

Var $ mResultArray; // The result array returned by the Select statement.


// _ Construct (), constructor to establish a database connection

Function _ construct ($ pHost, $ pUser, $ pPwd, $ pDbName ){
$ This-> mConnId = mysql_connect ($ pHost, $ pUser, $ pPwd); // establish a connection

Mysql_select_db ($ pDbName, $ this-> mConnId); // select a database

Mysql_query ("set names 'gbk'"); // sets the database encoding to gbk.

}

// _ Destruct: destructor, disconnected

Function _ destruct (){
Mysql_close ($ this-> mConnId); // There is a problem ......

}

// Execute the SQL statement

Function ExecuteSql (){
Mysql_query ($ this-> mSqlString );
}

// Query data. The returned value is an array of objects. Each element in the array is an object composed of a row of records.

Function Query (){
$ I = 0;
$ Query_result = mysql_query ($ this-> mSqlString, $ this-> mConnId );
While ($ row = mysql_fetch_object ($ query_result )){
$ This-> mResultArray [$ I ++] = $ row;
}
}

} // Class DataBase


// Use the following for testing:

$ Db = new DataBase ($ DBHOST, $ DBUSER, $ DBPWD, $ DBNAME );
$ Db-> mSqlString = "update student set phone = '000000' where id = '000000 '";
$ Db-> ExecuteSql ();
$ Db-> mSqlString = "select * from student where id = '000000 '";
$ Db-> Query ();
Print_r ($ db-> mResultArray); // output the test result

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.