We are in the development of the site more reasonable practice is that some of our commonly used programs to make a function or closed into a class, so that can be reused, can save development costs, I would like to introduce you to the class I often use.
Programmers who have an object-oriented technology base can read a day. In the case of PHP accessing the database, there are a lot of problems, such as character encoding problem, SQL syntax error problem, PHP processing data Record object and returning object problem, etc. I wrote here a database operation class, encapsulated the database additions and deletions and other operations, very convenient to use. With this class, you can accelerate the background development of the website.
Advantages:
1. Easy and fast, database operation only need to call interface;
2. Unified Coding (UTF8), not easy to cause garbled
3. Clear structure. such as Daemon (test.php) + Table wrapper class (user.class.php) + Database encapsulation Class (db.class.php) + configuration information (configuration.php) for front-end request processing
The following example has four files: configuration.php + db.class.php + user.class.php + test.php, placed in the same directory.
The first is a database configuration file class configuration.php
The code is as follows |
Copy Code |
/** * Database configuration information */ Define (' db_host ', ' localhost '); Server Define (' Db_user ', ' root '); Database user Name Define (' Db_password ', '); Database Password Define (' db_name ', ' test0 '); Default database Define (' Db_charset ', ' UTF8 '); Database Character Set Define (' TIMEZONE ', "PRC"); Time zone settings
?> |
Next is the database operations class db.class.php
The code is as follows |
Copy Code |
Require_once ("./configuration.php"); Introducing Configuration Constants Files Date_default_timezone_set (timezone); /** * Class Name: DB * Description: Database Operation class */ Class DB { Public $host; Server public $username; Database user Name Public $password; Data password Public $dbname; Database name public $conn; Database connection variables /** * DB Class constructors */ Public Function DB ($host =db_host, $username =db_user, $password =db_password, $dbname =db_name) { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; } /** * Open Database connection */ Public Function open () { $this->conn = mysql_connect ($this->host, $this->username, $this->password); mysql_select_db ($this->dbname); mysql_query ("Set CHARACTER set UTF8"); } /** * Close Data connection */ Public Function Close () { Mysql_close ($this->conn); } /** * Get data from SQL statements * @return: Array () */ Public Function Getobjlistbysql ($sql) { $this->open (); $rs = mysql_query ($sql, $this->conn); $objList = Array (); while ($obj = Mysql_fetch_object ($rs)) { if ($obj) { $objList [] = $obj; } } $this->close (); return $objList; } /** * Inserting data into a database table * @param: $table, table name * @param: $columns that contains an array of all the field names in the table. The default empty array is the full ordinal field name * @param: $values, contains an array of property values for all fields */ Public Function InsertData ($table, $columns =array (), $values =array ()) { $sql = ' INSERT INTO ' $table. ' ( '; for ($i = 0; $i < sizeof ($columns); $i + +) { $sql. = $columns [$i]; if ($i < sizeof ($columns)-1) { $sql. = ', '; } } $sql. = ') values ('; for ($i = 0; $i < sizeof ($values); $i + +) { $sql. = "'". $values [$i]. "'"; if ($i < sizeof ($values)-1) { $sql. = ', '; } } $sql. = ') '; $this->open (); mysql_query ($sql, $this->conn); $id = mysql_insert_id ($this->conn); $this->close (); return $id; } /** * Get data from a property in a table */ Public Function Getdatabyatr ($tableName, $atrName, $atrValue) { @ $data = $this->getobjlistbysql ("SELECT * from". $tableName. " WHERE $atrName = ' $atrValue ' "); if (count ($data)!=0) return $data; return NULL; } /** * Delete records by "id" in the table */ Public Function Delete ($tableName, $atrName, $atrValue) { $this->open (); $deleteResult = false; if (mysql_query ("DELETE from". $tableName. " WHERE $atrName = ' $atrValue ')) $deleteResult = true; $this->close (); if ($deleteResult) return true; else return false; } /** * Update the attribute values in the table */ Public Function Updateparambyid ($tableName, $atrName, $atrValue, $key, $value) { $db = new db (); $db->open (); if (mysql_query ("UPDATE". $tableName. " SET $key = ' $value ' WHERE $atrName = ' $atrValue ')) {//$key do not single quotation marks $db->close (); return true; } else{ $db->close (); return false; } } /* * @description: Gets all the property names of a table * @param: $tbName table name * @return: Array of strings */ Public Function FieldName ($tbName) { $resultName =array (); $i = 0; $this->open (); $result = mysql_query ("SELECT * from $tbName"); while ($property = Mysql_fetch_field ($result)) { $resultName [$i ++]= $property->name; } $this->close (); return $resultName; } } ?>
|
And then there's the test. I built a test0 database in phpMyAdmin and built a table user. Then use PHP to write a user table in the user class corresponding database.
user.class.php
The code is as follows |
Copy Code |
Require_once ("./db.class.php");
Class user{ Public $name = NULL; Public $password = NULL;
/** * Constructor function */ Public function __construct ($name, $password) { $this->name = $name; $this->password = $password; } Public Function Insert () { $db = new db (); $resultid = $db->insertdata ("User", Array (), Array (", $this->name, $this->password)); return $resultid; }
public static function Getuserbyid ($UID) { $db = new db (); return $db->getdatabyatr ("User", ' uid ', $uid); }
public static function Getuserbyname ($name) { $db = new db (); @ $data = $db->getobjlistbysql ("SELECT * from user WHERE name = ' $name '"); if (count ($data)!=0) return $data; else return null; } public static function Getalluser () { $db = new db (); @ $data = $db->getobjlistbysql ("SELECT * from user"); if (count ($data)!=0) return $data; else return null; }
public static function Deletebyuid ($UID) { $admin = Admin::getadminbyid ($uid); $db = new db (); if ($db->delete ("User", "UID", $uid)) return true; else return false; } }
?> |
Test procedure: test.php
The code is as follows |
Copy Code |
Header ("content-type:text/html; Charset=utf8 "); Require_once ("./user.class.php"); $user = New User ("HelloWorld", "123456"); $user->insert (); $users = User::getalluser (); foreach ($users as $u) { echo " ". $u->name." ". $u->password." "; } ?> |
http://www.bkjia.com/PHPjc/630692.html www.bkjia.com true http://www.bkjia.com/PHPjc/630692.html techarticle we are in the development of the site more reasonable practice is that some of our commonly used programs to make a function or closed into a class, so that can be reused, can save the development cost, I will give ...