Write your own MySQL class

Source: Internet
Author: User
Tags connect mysql mysql host php debug query resource return
Mysql

----------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)
$this->conntstr_ready = false; If one or more of these variables is assigned an empty string "", the preparation is not ready
Else
$this->conntstr_ready = true;
return $this->conntstr_ready;
}

}


 //Connect to the database, returning the 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



Related Article

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.