[I'm learning PHP III] PO on its own writing database class, easy to find later.

Source: Internet
Author: User

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.]

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.