Self-written mysql class

Source: Internet
Author: User
Tags mysql host php debug
---------- Phpdebug ---------- Serverlocalhost; DataBasemysql; UserIDroot; PassWord123456resource (5) oftype (mysqlresult) Outputcompleted (1 secconsumed)-NormalTermination

---------- Php debug ----------
Server = localhost; DataBase = mysql; UserID = root; PassWord = 123456
Resource (5) of type (mysql result)

Output completed (1 sec consumed)-Normal Termination

Class DBCLS
{

// Debug switch
Var $ debug = true;

// Debuginfo error message and debugging information
Var $ debuginfo = "debug informations :";

// Db_server MySQL host address
Var $ db_server;

// Db_name database name
Var $ db_name;

// Db_user database username
Var $ db_user;

// Db_passwd password
Var $ db_passwd;

// Db_links database connection
Var $ db_link;

// Db_query_ SQL SQL statement
Var $ db_query_ SQL = "show tables ";

// Db_recordset result set, record set
Var $ db_recordset;

// Ready switch
Var $ conntstr_ready = false;
Var $ link_ready = false;
Var $ db_ready = false;

// Set or return Connection String, or set the Connection String
Function connection_string ($ connstr = "")
{
If ("" = $ connstr)
{
// If no parameter is input, an existing connection character is returned.
Return "Server = ". $ this-> db_server. "; DataBase = ". $ this-> db_name. "; UserID = ". $ this-> db_user. "; PassWord = ". $ this-> db_passwd;
}
Else
{
// Otherwise, parse the connection character and initialize the variable
Preg_match_all ("| ([a-zA-Z0-9] *) = ([a-zA-Z0-9] *); * |", $ connstr, $ tmparr, PREG_PATTERN_ORDER );
$ This-> db_server = (strtolower ($ tmparr [1] [0]) = "server ")? $ Tmparr [2] [0]: "";
$ This-> db_name = (strtolower ($ tmparr [1] [1]) = "database ")? $ Tmparr [2] [1]: "";
$ This-> db_user = (strtolower ($ tmparr [1] [2]) = "userid ")? $ Tmparr [2] [2]: "";
$ This-> db_passwd = (strtolower ($ tmparr [1] [3]) = "password ")? $ Tmparr [2] [3]: "";
// Var_dump ($ tmparr );

If ("" = $ this-> db_server or "" = $ this-> db_name or "" = $ this-> db_user or "" = $ this-> db_passwd)
$ This-> conntstr_ready = false; // if more than one of the variables is assigned an empty string "", the preparation is not ready.
Else
$ This-> conntstr_ready = true;
Return $ this-> conntstr_ready;
}

}


// Connect to the database and return the connection object
Function connect ()
{
If (! $ This-> conntstr_ready)
{
If ($ this-> debug) {$ this-> debuginfo. = "The connection string is invalid! ";}
Return $ this-> link_ready;
}
If ($ this-> db_link = @ mysql_connect ($ this-> db_server, $ this-> db_user, $ this-> db_passwd ))
{
$ This-> link_ready = true;
Return $ this-> link_ready;
}
}


// Select a database
Function select ($ db = "")
{
If (! $ This-> db_link)
{
If ($ this-> debug) {$ this-> debuginfo. = "no available database connection! ";}
$ This-> db_ready = false;
}
If ("" = $ db)
{
If (mysql_select_db ($ this-> db_name ))
$ This-> db_ready = true;
}
If (mysql_select_db ($ this-> db_name = $ db ))
$ This-> db_ready = true;
Return $ this-> db_ready;
}


// Execute the query
Function execute ($ SQL = "")
{
If (""! = $ SQL)
{
$ This-> db_query_ SQL = $ SQL;
// If ($ this-> debug) {$ this-> debuginfo. = "SQL is null! ";}
// Return false;
}
If ("" = $ this-> db_query_ SQL)
{
If ($ this-> debug) {$ this-> debuginfo. = "SQL is null! ";}
Return false;
}
$ This-> db_recordset = mysql_query ($ this-> db_query_ SQL );
Return true;
}
}


$ Dbmy = new DBCLS (); // Create a DBCLS object
If (! $ Dbmy-> connection_string ("Server = localhost; DataBase = mysql; UserID = root; PassWord = 123456") // initialize the DBCLS object dbmy

Print $ dbmy-> debuginfo; // initialization failed, error message output

Else

Print $ dbmy-> connection_string (); // The initialization is successful and the connection string is output.
Print "";

If (! ($ Dbmy-> connect () or! ($ Dbmy-> select () // if the database is connected or the database selection fails
Print $ dbmy-> debuginfo; // output error message
// $ Dbmy-> db_query_ SQL = "select * from user ";
If ($ dbmy-> execute () // execute the query command
{// If no error occurs
Var_dump ($ dbmy-> db_recordset); // output the returned result set.
}
?>

Output information:

---------- Php debug ----------
Server = localhost; DataBase = mysql; UserID = root; PassWord = 123456
Resource (5) of type (mysql result)

Output completed (1 sec consumed)-Normal Termination


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.