Class mysql{private $conn;//privatization variable $conn private $db _host;//host Private $db _user; Private $db _password; Private $db _name; Database name function __construct ($db _host, $db _user, $db _password, $db _name) {//constructor, passed in to the Connect function in class $this, Db_host = $db _host; $this->db_user = $db _user; $this->db_password = $db _password; $this->db_name = $db _name; $this->connect (); The function connect () {//Database connection function $this->conn = mysql_connect ($this->db_host, $this->db_user, $this- >db_password) or Die ("Database connection Failed". Mysql_errno (). ":". Mysql_error ()); mysql_select_db ($this->db_name, $this->conn) or Die (' Open database Failed '). Mysql_error (); Mysql_set_charset (' UTF8 '); return $this->conn; }//Insert database, $table (database name), $array function Insert ($table, $array) {$keys = "'". Implode ("', '", Array_keys ($array)) ." `"; The key values in the array are $vals = "'". Implode ("', '", Array_values ($array)). "'"; Adjust the values in the array $sql= "INSERT INTO {$table} ({$keys}) VALUES ({$vals})"; $query = mysql_query ($sql); return mysql_insert_id (); }//delete specified data function delete ($table, $id, $where =null) {//statement: Delete from table where id = .... $where = $ where ==null?null: ' where '. $where; $sql = "Delete from {$table} where id = {$id} limit 1"; $query = mysql_query ($sql); return $query; }//Select Database Public Function Select ($table, $array, $where =null) {//statement: SELECT * from table where ' user ' = ' $user '. .... foreach ($array as $key + $value) {$select [] = '. $key. ' ' = '. $value; } $select = Implode (' and ', $select); $where = $where = = Null?null: $where; $sql = "Select * FROM {$table} where". $select. ' '. $where; return $sql; }//Modify Database function Update ($table, $array, $where = null) {//Statement: Update table set ' name ' = ' Kopa ' WHERE id = .... foreach ($array as $key = = $value) {$string [] =". $key. ' ' = '. $value; } $string =implode (', ', $string); $where = $where ==null?null: "where". $where; $sql = "Update {$table} set". $string. $where; return $sql; $query = mysql_query ($sql); return $query; Print_r ("Update {$table} set". $string. $where); }//Reads the total number of database rows Mysql_num_row function TotalRow ($sql) {$query = mysql_query ($sql); $result = mysql_num_rows ($query); return $result; }//Read array of database function Fetch_array ($sql) {$query = mysql_query ($sql); $res = Mysql_fetch_array ($query); return $res; }} $db = new MySQL ("localhost", ' root ', ' 3363064 ', ' ctxy ');
[I'm learning my own database class on PHP three]po to make it easier to find later.]