----------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 Debugging Switch
var $debug = true;
Debuginfo error messages, debugging information
var $debuginfo = "Debug informations:";
Db_server MySQL host address
var $db _server;
Db_name database name
var $db _name;
Db_user Database user Name
var $db _user;
DB_PASSWD Password
var $db _passwd;
Db_links Database Connection
var $db _link;
Db_query_sql SQL statements
var $db _query_sql = "Show Tables";
Db_recordset result set, recordset
var $db _recordset;
Ready switch
var $conntstr _ready = false;
var $link _ready = false;
var $db _ready = false;
Set or return Connection string return, or set connection string
function connection_string ($connstr = "")
{
if ("" = = $connstr)
{
Returns an existing connection character if no arguments are passed in
Return "server=". $this->db_server. ";D Atabase= ". $this->db_name."; Userid= ". $this->db_user."; Password= ". $this->db_passwd;
}
Else
{
Otherwise, parse the connection character, 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 [note]) = = "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 database, return 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 Database
function Select ($db = "")
{
if (! $this->db_link)
{
if ($this->debug) {$this->debuginfo. = "No database connection available! ";}
$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 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 Dbcls object dbmy
Print $dbmy->debuginfo; Initialization failed with output error message
Else
Print $dbmy->connection_string (); Initialization succeeded, output connection string
Print "";
if (! ( $dbmy->connect ()) or! ($dbmy->select ())) If you connect to the database or select a database failure
Print $dbmy->debuginfo; Output error message
$dbmy->db_query_sql = "SELECT * from user";
if ($dbmy->execute ())//Execute Query instruction
{//If there is no error
Var_dump ($dbmy->db_recordset);//output 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
http://www.bkjia.com/PHPjc/532641.html www.bkjia.com true http://www.bkjia.com/PHPjc/532641.html techarticle ----------php debug----------server=localhost;database=mysql; Userid=root; password=123456 resource (5) of type (MySQL result) Output completed (1 sec consumed)-Normal terminatio ...