Phpmysql database connection class _ PHP Tutorial

Source: Internet
Author: User
Phpmysql database connection class. A simple connection class is provided to automatically create and delete connections using php constructor. if you need it, you can refer to it. The following code copies the classmysql {private $ d code to provide a simple connection class that uses the php constructor to automatically create and delete connections. For more information, see.
The code is as follows:

Class mysql {
Private $ db_host; // database host
Private $ db_user; // database username
Private $ db_pwd; // database username and password
Private $ db_database; // database name
Private $ conn; // Database Connection ID;
Private $ result; // result resource ID for executing the query command
Private $ SQL; // SQL execution statement
Private $ row; // number of returned entries
Private $ coding; // database encoding, GBK, UTF8, gb2312
Private $ bulletin = true; // whether to enable error records
Private $ show_error = true; // in the test phase, all errors are displayed, which has security risks and is disabled by default.
Private $ is_error = false; // determines whether an error is terminated immediately. the default value is true. we recommend that you do not enable the error because it is annoying to see nothing when there is a problem.

/* Constructor */
Public function _ construct ($ db_host, $ db_user, $ db_pwd, $ db_database, $ conn, $ coding ){
$ This-> db_host = $ db_host;
$ This-> db_user = $ db_user;
$ This-> db_pwd = $ db_pwd;
$ This-> db_database = $ db_database;
$ This-> conn = $ conn;
$ This-> coding = $ coding;
$ This-> connect ();
}

/* Database connection */
Public function connect (){
If ($ this-> conn = "pconn "){
// Permanent link
$ This-> conn = mysql_pconnect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
} Else {
// Even if the link
$ This-> conn = mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
}

If (! Mysql_select_db ($ this-> db_database, $ this-> conn )){
If ($ this-> show_error ){
$ This-> show_error ("database unavailable:", $ this-> db_database );
}
}
Mysql_query ("set names $ this-> coding ");
}

/* Database execution statements: execute queries, add, modify, delete, and other SQL statements */
Public function query ($ SQL ){
If ($ SQL = ""){
$ This-> show_error ("SQL statement error:", "SQL query statement is empty ");
}
$ This-> SQL = $ SQL;

$ Result = mysql_query ($ this-> SQL, $ this-> conn );

If (! $ Result ){
// Used during debugging. the SQL statement is automatically printed when an error occurs.
If ($ this-> show_error ){
$ This-> show_error ("incorrect SQL statement:", $ this-> SQL );
}
} Else {
$ This-> result = $ result;
}
Return $ this-> result;
}

/* Create and add a new database */
Public function create_database ($ database_name ){
$ Database = $ database_name;
$ SqlDatabase = 'create database'. $ database;
$ This-> query ($ sqlDatabase );
}

The sequence constructor automatically creates connections and deletes operations. For more information, see. The code is as follows: class mysql {private $ d...

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.