Php database mysql Query and connection class _ PHP Tutorial

Source: Internet
Author: User
Tags php database
Php database mysql Query and connection class .? Phpclassmysql {var $ host; mysql host name var $ user; mysql username var $ pwd; mysql password var $ dbName; mysql database name var $ linkID0; used to save connection I


Class mysql
{
Var $ host = ""; // mysql host name
Var $ user = ""; // mysql user name
Var $ pwd = ""; // mysql password
Var $ dbName = ""; // mysql database name
Var $ linkID = 0; // used to save the Connection ID
Var $ queryID = 0; // used to save the query ID
Var $ fetchMode = MYSQL_ASSOC; // mode in which records are Retrieved
Var $ queryTimes = 0; // Number of times the query is saved
Var $ errno = 0; // mysql error code
Var $ error = ""; // mysql error message
Var $ record = array (); // an array of records

// ================================================
// Function: mysql ()
// Function: constructor
// Parameter: variable definition of the parameter class
// Description: The constructor will automatically connect to the database.
// Remove the connection function if you want to manually connect
// ================================================
Function mysql ($ host, $ user, $ pwd, $ dbName)
{If (empty ($ host) | empty ($ user) | empty ($ dbName ))
$ This-> halt ("The database host address, username or database name is incomplete. Please check! ");
$ This-> host = $ host;
$ This-> user = $ user;
$ This-> pwd = $ pwd;
$ This-> dbName = $ dbName;
$ This-> connect (); // set to automatic connection
}
// ================================================
// Function: connect ($ host, $ user, $ pwd, $ dbName)
// Function: connect to the database
// Parameter: $ host name, $ user username
// Parameter: $ pwd password, $ dbName database name
// Return value: 0: Failed
// Description: the initial values of variables in the class are used by default.
// ================================================
Function connect ($ host = "", $ user = "", $ pwd = "", $ dbName = "")
{
If ("" = $ host)
$ Host = $ this-> host;
If ("" = $ user)
$ User = $ this-> user;
If ("" = $ pwd)
$ Pwd = $ this-> pwd;
If ("" = $ dbName)
$ DbName = $ this-> dbName;
// Now connect to the database
$ This-> linkID = mysql_pconnect ($ host, $ user, $ pwd );
If (! $ This-> linkID)
{
$ This-> halt ();
Return 0;
}
If (! Mysql_select_db ($ dbName, $ this-> linkID ))
{
$ This-> halt ();
Return 0;
}
Return $ this-> linkID;
}
// ================================================
// Function: query ($ SQL)
// Function: Data Query
// Parameter: $ SQL statement to be queried
// Return value: 0: Failed
// ================================================
Function query ($ SQL)
{
$ This-> queryTimes ++;
$ This-> queryID = mysql_query ($ SQL, $ this-> linkID );
If (! $ This-> queryID)
{
$ This-> halt ();
Return 0;
}
Return $ this-> queryID;
}
// ================================================
// Function: setFetchMode ($ mode)
// Function: Set the mode for obtaining records
// Parameter: $ mode MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
// Return value: 0: Failed
// ================================================
Function setFetchMode ($ mode)
{
If ($ mode = MYSQL_ASSOC | $ mode = MYSQL_NUM | $ mode = MYSQL_BOTH)
{
$ This-> fetchMode = $ mode;
Return 1;
}
Else
{
$ This-> halt ("wrong mode .");
Return 0;
}

}
// ================================================
// Function: fetchRow ()
// Function: retrieve a record from the record set
// Return: 0: Error record: A record
// ================================================
Function fetchRow ()
{
$ This-> record = mysql_fetch_array ($ this-> queryID, $ this-> fetchMode );
Return $ this-> record;
}
// ================================================
// Function: fetchAll ()
// Function: retrieve all records from the record set
// Return: number of record sets
// ================================================
Function fetchAll ()
{
$ Arr = array ();
While ($ this-> record = mysql_fetch_array ($ this-> queryID, $ this-> fetchMode ))
{
$ Arr [] = $ this-> record;
}
Mysql_free_result ($ this-> queryID );
Return $ arr;
}
// ================================================
// Function: getValue ()
// Function: return the data of the specified field in the record.
// Parameter: $ field name or field index
// Return: the value of the specified field
// ================================================
Function getValue ($ field)
{
Return $ this-> record [$ field];
}
// ================================================
// Function: affectedRows ()
// Function: return the number of affected records
// ================================================
Function affectedRows ()
{
Return mysql_affected_rows ($ this-> linkID );
}
// ================================================
// Function: recordCount ()
// Function: returns the total number of query records.
// Parameter: None
// Return: total number of records
// ================================================
Function recordCount ()
{
Return mysql_num_rows ($ this-> queryID );
}
// ================================================
// Function: getQueryTimes ()
// Function: return the number of queries
// Parameter: None
// Return: number of queries
// ================================================
Function getQueryTimes ()
{
Return $ this-> queryTimes;
}
// ================================================
// Function: getVersion ()
// Function: returns the mysql version.
// Parameter: None
// ================================================
Function getVersion ()
{
$ This-> query ("select version () as ver ");
$ This-> fetchRow ();
Return $ this-> getValue ("ver ");
}
// ================================================
// Function: getDBSize ($ dbName, $ tblPrefix = null)
// Function: returns the space occupied by the database.
// Parameter: $ dbName database name
// Parameter: prefix of the $ tblPrefix table. optional.
// ================================================
Function getDBSize ($ dbName, $ tblPrefix = null)
{
$ SQL = "SHOW TABLE STATUS FROM". $ dbName;
If ($ tblPrefix! = Null ){
$ SQL. = "LIKE '$ tblPrefix % '";
}
$ This-> query ($ SQL );
$ Size = 0;
While ($ this-> fetchRow ())
$ Size + = $ this-> getValue ("Data_length") + $ this-> getValue ("Index_length ");
Return $ size;
}
// ================================================
// Function: insertID ()
// Function: returns the ID of the last inserted auto-increment.
// Parameter: None
// ================================================
Function insertID (){
Return mysql_insert_id ();
}
// ================================================
// Function: halt ($ err_msg)
// Function: process all error messages
// Parameter: $ err_msg custom error message
// ==============================================
Function halt ($ err_msg = "")
{
If ("" = $ err_msg)
{
$ This-> errno = mysql_errno ();
$ This-> error = mysql_error ();
Echo"Mysql error:
";
Echo $ this-> errno. ":". $ this-> error ."
";
Exit;
}
Else
{
Echo"Mysql error:
";
Echo $ err_msg ."
";
Exit;
}
}
}
?>

Http://www.bkjia.com/PHPjc/630438.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630438.htmlTechArticle? Php class mysql {var $ host =; // mysql host name var $ user =; // mysql username var $ pwd =; // mysql password var $ dbName =; // mysql database name var $ linkID = 0; // used to save the connection I...

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.