Php calls mysql data dbclass _ PHP Tutorial

Source: Internet
Author: User
Php calls the dbclass class of mysql data. Copy the code as follows: classdbClass {start database Class var $ username; var $ password; var $ database; var $ hostname; var $ link; var $ result; functiondbClass ($ username, $ passw The code is as follows:


Class dbClass {// start Database class
Var $ username;
Var $ password;
Var $ database;
Var $ hostname;
Var $ link;
Var $ result;

Function dbClass ($ username, $ password, $ database, $ hostname = "localhost "){
$ This-> username = $ username;
$ This-> password = $ password;
$ This-> database = $ database;
$ This-> hostname = $ hostname;
}

Function connect () {// This function is used to connect to the database
If (! $ This-> link = mysql_connect ($ this-> hostname, $ this-> username, $ this-> password ))
$ This-> halt ("Sorry, can not connect to database ");

If ($ this-> version ()> '4. 1 '){
Global $ dbcharset, $ charset;
If (! $ Dbcharset & in_array (strtolower ($ charset), array ('gbk', 'big5', 'utf-8 '))){
$ Dbcharset = str_replace ('-', '', $ charset );
}
If ($ dbcharset ){
Mysql_query ("SET character_set_connection = $ dbcharset, character_set_results = $ dbcharset, character_set_client = binary ");
}
}
If ($ this-> version ()> '5. 0.1 '){
Mysql_query ("SET SQL _mode = ''");
}

Return $ this-> link;
}

Function select () {// This function is used to select a database
Mysql_select_db ($ this-> database, $ this-> link );
}

Function query ($ SQL) {// This function is commonly used to send query statements and return results.
If ($ this-> result = mysql_query ($ SQL, $ this-> link) return $ this-> result;
Else {
$ This-> halt ("SQL statement error: $ SQL

Error message: ". mysql_error ());
Return false;
}
}

/*
The following functions are used to retrieve an array from the result. they are generally used with the while () loop and $ db-> query ($ SQL) functions, for example:
$ Result = query ("select * from mytable ");
While ($ row = $ db-> getarray ($ result )){
Echo "$ row [id]";
}
*/
Function getarray ($ result ){
Return @ mysql_fetch_array ($ result );
}

/*
The following function is used to obtain the first row of an SQL query. it is generally used to query whether a qualified row exists. for example:
The username $ username and password $ password submitted by the user from the form are in the user table "user", and the corresponding array is returned:
If ($ user = $ db-> getfirst ("select * from user where username = '$ username' and password =' $ password '"))
Echo "welcome $ username. your ID is $ user [id]. ";
Else
Echo "incorrect user name or password! ";
*/
Function getfirst ($ SQL ){
Return @ mysql_fetch_array ($ this-> query ($ SQL ));
}

/*
The following function returns the total number of rows that meet the query conditions, for example, used for paging calculation. for example:
$ Totlerows = $ db-> getcount ("select * from mytable ");
Echo "a total of $ totlerows information. ";
*/
Function getcount ($ SQL ){
Return @ mysql_num_rows ($ this-> query ($ SQL ));
}

/*
Use the following functions to update a database, such as changing the password:
$ Db-> update ("update user set password = '$ new_password' where userid = '$ userid '");
*/
Function update ($ SQL ){
Return $ this-> query ($ SQL );
}

/*
The following function is used to insert a row to a database, for example, adding a user:
$ Db-> insert ("insert into user (userid, username, password) values (null, '$ username',' $ password ')");
*/
Function insert ($ SQL ){
Return $ this-> query ($ SQL );
}

Function getid () {// This function is used to obtain the id of the inserted row.
Return mysql_insert_id ();
}

Function num_rows ($ query ){
$ Query = mysql_num_rows ($ query );
Return $ query;
}

Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}

Function free_result ($ query ){
Return mysql_free_result ($ query );
}

Function version (){
Return mysql_get_server_info ();
}

Function close (){
Return mysql_close ();
}

Function halt ($ message = ''){
Return $ message;
}
}

$ Db = new dbClass ("$ db_username", "$ db_password", "$ db_database", "$ db_hostname ");
$ Db-> connect ();
$ Db-> select ();

The http://www.bkjia.com/PHPjc/323376.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323376.htmlTechArticle code is as follows: class dbClass {// start database class var $ username; var $ password; var $ database; var $ hostname; var $ link; var $ result; function dbClass ($ username, $ passw...

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.