----------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
{
#----------------------------------------------------
# Database Control class Dbcls
# Author: yms
# yms541@yahoo.com.cn
# 2005-5-30
# 2005-5-31
# php_v5.0.1
# EditPlus Text editer v2.12 (76)
#---------------------------------
Debug Debug Switch
var $debug = true;
Debuginfo error messages, debugging information
var $debuginfo = "Debug informations:\r\n";
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 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)
{
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 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)
$th Is->conntstr_ready = false; If one of the above variables is assigned an empty string "", prepare to not be ready
Else
$this->conntstr_ready = true;
return $this->conntstr_ready;
}
}
//Connection database, return Connection object
Function connect ()
{
if (! $this->conntstr_ready)
{
if ($this->debug) { $this->debuginfo. = "Connection string is invalid!" \ r \ n ";}
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 is available!" \ r \ n ";}
$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!\r\n";}
return false;
}
if ("" = = $this->db_query_sql)
{
if ($this->debug) {$this->debuginfo. = "SQL is null!\r\n";}
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, output error message
Else
Print $dbmy->connection_string (); Initialization succeeded, output connection string
print "\ r \ n";
if (!) ( $dbmy->connect ()) or! ($dbmy->select ())) If you connect to a 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