MySQL Database Class Definition

Source: Internet
Author: User

As the saying goes, "a good start is half the success", and database operations in the PHP + MySQL project are one of the key points. Whether or not to simplify the writing of database operation programs has become one of the keys that affect work efficiency.


Therefore, Xiaoyang does not create a page from the very beginning, but first creates a "dbclass. php" file to write the class "dbClass" for operating MySQL databases ". Write the following program in "dbclass. php:

<? Php

$ Db_username = "myusername"; // username used to connect to the database
$ Db_password = "mypassword"; // password used to connect to the database
$ Db_database = "mydatabase"; // Database Name
$ Db_hostname = "localhost"; // server address

Class dbClass {// start Database class
Var $ username;
Var $ password;
Var $ database;
Var $ hostname;
Var previous directory next;
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
$ This-> link = mysql_connect ($ this-> hostname, $ this-> username, $ this-> password) or die ("Sorry, can not connect to database ");
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 {
// The error message of the SQL statement is displayed here, which is mainly used for prompts during the design phase. You can comment out the following sentence in the formal operation phase.
Echo "SQL statement error: <font color = red> $ SQL </font> <BR> 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 ();
}
}

/*
These are the main functions. If you have other needs, you can add them yourself.
If you use this class, you must connect to the database. Next, connect and select the database:
*/
$ Db = new dbClass ("$ db_username", "$ db_password", "$ db_database", "$ db_hostname ");
$ Db-> connect ();
$ Db-> select ();

?>

OK. The database class has been written. It can be used not only in the current project, but also in other projects! Just copy "dbclass. php. To use this file, you only need to use the statement "include_once (" dbclass. php ")". The specific syntax has examples when writing database classes and will not be repeated.

After writing the database class, database operations are much easier, and project creation has taken an important first step.

As the saying goes, "a good start is half the success", and database operations in the PHP + MySQL project are one of the key points. Whether or not to simplify the writing of database operation programs has become one of the keys that affect work efficiency.


Therefore, Xiaoyang does not create a page from the very beginning, but first creates a "dbclass. php" file to write the class "dbClass" for operating MySQL databases ". Write the following program in "dbclass. php:

<? Php

$ Db_username = "myusername"; // username used to connect to the database
$ Db_password = "mypassword"; // password used to connect to the database
$ Db_database = "mydatabase"; // Database Name
$ Db_hostname = "localhost"; // server address

Class dbClass {// start Database class
Var $ username;
Var $ password;
Var $ database;
Var $ hostname;
Var previous directory next;
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
$ This-> link = mysql_connect ($ this-> hostname, $ this-> username, $ this-> password) or die ("Sorry, can not connect to database ");
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 {
// The error message of the SQL statement is displayed here, which is mainly used for prompts during the design phase. You can comment out the following sentence in the formal operation phase.
Echo "SQL statement error: <font color = red> $ SQL </font> <BR> 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 ();
}
}

/*
These are the main functions. If you have other needs, you can add them yourself.
If you use this class, you must connect to the database. Next, connect and select the database:
*/
$ Db = new dbClass ("$ db_username", "$ db_password", "$ db_database", "$ db_hostname ");
$ Db-> connect ();
$ Db-> select ();

?>

OK. The database class has been written. It can be used not only in the current project, but also in other projects! Just copy "dbclass. php. To use this file, you only need to use the statement "include_once (" dbclass. php ")". The specific syntax has examples when writing database classes and will not be repeated.

After writing the database class, database operations are much easier, and project creation has taken an important first step.

As the saying goes, "a good start is half the success", and database operations in the PHP + MySQL project are one of the key points. Whether or not to simplify the writing of database operation programs has become one of the keys that affect work efficiency.


Therefore, Xiaoyang does not create a page from the very beginning, but first creates a "dbclass. php" file to write the class "dbClass" for operating MySQL databases ". Write the following program in "dbclass. php:

<? Php

$ Db_username = "myusername"; // username used to connect to the database
$ Db_password = "mypassword"; // password used to connect to the database
$ Db_database = "mydatabase"; // Database Name
$ Db_hostname = "localhost"; // server address

Class dbClass {// start Database class
Var $ username;
Var $ password;
Var $ database;
Var $ hostname;
Var previous directory next;
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
$ This-> link = mysql_connect ($ this-> hostname, $ this-> username, $ this-> password) or die ("Sorry, can not connect to database ");
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 {
// The error message of the SQL statement is displayed here, which is mainly used for prompts during the design phase. You can comment out the following sentence in the formal operation phase.
Echo "SQL statement error: <font color = red> $ SQL </font> <BR> 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 ();
}
}

/*
These are the main functions. If you have other needs, you can add them yourself.
If you use this class, you must connect to the database. Next, connect and select the database:
*/
$ Db = new dbClass ("$ db_username", "$ db_password", "$ db_database", "$ db_hostname ");
$ Db-> connect ();
$ Db-> select ();

?>

OK. The database class has been written. It can be used not only in the current project, but also in other projects! Just copy "dbclass. php. To use this file, you only need to use the statement "include_once (" dbclass. php ")". The specific syntax has examples when writing database classes and will not be repeated.

After writing the database class, database operations are much easier, and project creation has taken an important first step.

 

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.