File name: mysql. class. php & lt ;? // ####################### StartIntroduce ############### #########################// mysql connection class // author: bluemaple, emaile: bluemaple@x263.net // executable
File name: mysql. class. php
// ####################### Start Introduce ############## #########################
// Mysql connection class
// Author: bluemaple, emaile: bluemaple@x263.net
// You can run general mysql commands, such as insert, delete, select, and update.
// Usage: before the required File
// Require ("./mysql. class. php ");
// $ DB = new DB_MYSQL; // load class
// $ DB-> dbServer = "localhost"; // connection address
// $ DB-> dbUser = "root"; // User name
// $ DB-> dbPwd = ""; // password
// $ DB-> dbDatabase = "we"; // database name
// $ DB-> connect (); // connect to the database
// You can change the database in use.
// Function description
// Query ($ SQL, $ dbbase); // you can directly execute
// Query_first ($ SQL, $ dbbase); // only one record is returned for the query. $ SQL is an SQL statement, and $ dbbase is the database selected for you (optional)
// Fetch_array ($ SQL, $ dbbase); // A group of records are returned for query. you can use num_rows to obtain the returned number.
// Insert, update, and delete are all execution commands. $ affected_rows is available.
// You can use insert_id to obtain the number of IDS returned for the insert result.
// Count_records ($ table, $ index, $ where, $ dbbase) // returns the number of records for a table, $ table indicates the table name, and $ index indicates the key, $ where is the condition, $ dbbase is the database, and the last two can be deselected
// ######################## End Introduce ############# ###########################
Class DB_MYSQL // class for querying database mysql
{
Var $ dbServer; // database connection service address
Var $ dbDatabase; // Selected database, initial state
Var $ dbbase = ""; // can be changed later
Var $ dbUser; // login user name
Var $ dbPwd; // login user password
Var $ dbLink; // database connection pointer
Var $ query_id; // pointer for executing the query command
Var $ num_rows; // number of returned entries
Var $ insert_id; // returns the ID of the last INSERT command.
Var $ affected_rows; // number of columns affected by the query command returned
// The number of columns (row) affected by INSERT, UPDATE, or DELETE.
// If delete does not contain the where clause, 0 is returned.
Function connect ($ dbbase = "") // connect to a database function, including connecting to a database
{
Global $ usepconnect; // whether to use permanent connection. $ userpconnect is set externally.
If ($ usepconnect = 1 ){
$ This-> dbLink = @ mysql_pconnect ($ this-> dbServer, $ this-> dbUser, $ this-> dbPwd );
} Else {
$ This-> dbLink = @ mysql_connect ($ this-> dbServer, $ this-> dbUser, $ this-> dbPwd );
}
If (! $ This-> dbLink) $ this-> halt ("connection error, unable to connect !!! ");
If ($ dbbase = ""){
$ Dbbase = $ this-> dbDatabase;
}
If (! Mysql_select_db ($ dbbase, $ this-> dbLink) // connect to the database
{$ This-> halt ("this database cannot be used. Please check whether this database is correct !!! ");}
}
Function change_db ($ dbbase = "") {// change the database
$ This-> connect ($ dbbase );
}
Function query_first ($ SQL, $ dbbase = "") {// SQL command for returning a value
$ Query_id = $ this-> query ($ SQL, $ dbbase );
$ Returnarray = mysql_fetch_array ($ query_id );
$ This-> num_rows = mysql_num_rows ($ query_id );
$ This-> free_result ($ query_id );
Return $ returnarray;
}
Function fetch_array ($ SQL, $ dbbase = "", $ type = 0) {// SQL command that returns a value
// Type indicates whether the passed value is name => value, or 4 => value
$ Query_id = $ this-> query ($ SQL, $ dbbase );
$ This-> num_rows = mysql_num_rows ($ query_id );
For ($ I = 0; $ I <$ this-> num_rows; $ I ++ ){
If ($ type = 0)
$ Array [$ I] = mysql_fetch_array ($ query_id );
Else
$ Array [$ I] = mysql_fetch_row ($ query_id );
}
$ This-> free_result ($ query_id );
Return $ array;
}
Function delete ($ SQL, $ dbbase = "") {// delete command
$ Query_id = $ this-> query ($ SQL, $ dbbase );
$ This-> affected_rows = mysql_affected_rows ($ this-> dbLink );
$ This-> free_result ($ query_id );
}
Function insert ($ SQL, $ dbbase = "") {// insert command
$ Query_id = $ this-> query ($ SQL, $ dbbase );
$ This-> insert_id = mysql_insert_id ($ this-> dbLink );
$ This-> affected_rows = mysql_affected_rows ($ this-> dbLink );
$ This-> free_result ($ query_id );
}
Function update ($ SQL, $ dbbase = "") {// update command
$ Query_id = $ this-> query ($ SQL, $ dbbase );
$ This-> affected_rows = mysql_affected_rows ($ this-> dbLink );
$ This-> free_result ($ query_id );
}
Function count_records ($ table, $ index = "id", $ where = "", $ dbbase = "") {// record the total number of tables
// Where is the condition
// Dbbase is the database
// Index indicates the selected key. the default value is id.
If ($ dbbase! = "") $ This-> change_db ($ dbbase );
$ Result = @ mysql_query ("select count (". $ index. ") as 'num' from $ table". $ where, $ this-> dbLink );
If (! $ Result) $ this-> halt ("incorrect SQL statement:". $ SQL );
@ $ Num = mysql_result ($ result, 0, "num ");
Return $ num;
}
Function query ($ SQL, $ dbbase = "") {// execute the queyr command
If ($ dbbase! = "") $ This-> change_db ($ dbbase );
$ This-> query_id = @ mysql_query ($ SQL, $ this-> dbLink );
Echo "d ";
If (! $ This-> query_id) $ this-> halt ("incorrect SQL statement:". $ SQL );
Return $ this-> query_id;
}
Function halt ($ errmsg) // database error. connection failed
{
$ Msg ="Database error!
";
$ Msg. = $ errmsg;
Echo $ msg;
Die ();
}
Function free_result ($ query_id) // release the query selector
{
@ Mysql_free_result ($ query_id );
}
Function close () // close the database connection
{
Mysql_close ($ this-> dbLink );
}
}
?>
Use
Text. php
Require ("./mysql. class. php ");
$ DB = new DB_MYSQL;
$ DB-> dbServer = "localhost ";
$ DB-> dbUser = "root ";
$ DB-> dbPwd = "";
$ DB-> dbDatabase = "we ";
$ DB-> connect (); // connect to the database
?>
From the continent of Magic Spring