A more reasonable approach when we develop websites is to make some of our commonly used programs into functions or close them into classes, so that they can be reused and save development costs. Let me introduce to you the classes that I often use. .
Programmers with a foundation in object-oriented technology can start writing it in just one day. When PHP accesses the database, various problems often occur, such as character encoding problems, SQL syntax errors, PHP processing data record objects and return objects, etc. I have written a database operation class here, which encapsulates operations such as addition, deletion, addition, and modification of the database, which is very convenient to use. Using this class can speed up the backend development of the website.
Advantages:
1. Convenient and fast, database operations only need to call the interface;
2. Unified encoding (utf8), not easy to cause garbled characters
3. Clear structure. For example, the background program that handles front-end requests (test.php) + table encapsulation class (user.class.php) + database encapsulation class (db.class.php) + configuration information (configuration.php)
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 | ||||
|
The next step is the database operation class db.class.php
The code is as follows | Copy code |
require_once("./configuration.php"); //Introduce configuration constant file date_default_timezone_set(TIMEZONE); /** * Class name: DB * Description: Database operation class */ class DB { public $host; //Server public $username; //Database username public $password; //Data password public $dbname; //Database name public $conn; //Database connection variables /** * DB class constructor */ 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 through sql statement * @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; } /** * Insert data into database table * @param:$table, table name * @param: $columns, an array containing all field names in the table. The default empty array is all ordered field names * @param: $values, an array containing attribute values corresponding to 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 through an attribute in the 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 through "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 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不要单引号 $db->close(); return true; } else{ $db->close(); return false; } } /* * @description: 取得一个table的所有属性名 * @param: $tbName 表名 * @return:字符串数组 */ 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; } } ?> |
The next step is testing. I built a test0 database in phpmyadmin, and created a table user in it. Then use PHP to write a user class corresponding to the user table in the database.
user.class.php
The code is as follows
|
Copy code
|
||||
|