---------- Php debug ----------
Server = localhost; DataBase = mysql; UserID = root; PassWord = 123456
Resource (5) of type (mysql result)
Output completed (1 sec consumed)-Normal Termination
<? Php
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