PHP write MySQL operation class

Source: Internet
Author: User
Tags mysql host php write

File:MySQL.class.PHP<?PHPerror_reporting(E_all^e_deprecated);/*MySQL Operation class * 1. Connect to the database * 2. Select the library that needs to be manipulated * 3. Set the encoding of the operation * 4. Search and delete * Query in two cases: * 1. Query one data: Execute once mysql_fetch_assoc* 2. Query multi-hop data: Perform multiple Mysql_ fetch_assoc* 5. Closing the database*/class MySQL { Public $hostname;//MySQL host Public $username;//MySQL user name Public $password;//mysql Password Public $conn;//Connection Identifiers Public $dbname;//database name of the operation Public $charset;//Action encoding//initializationfunction__construct ($hostname,$username,$password,$dbname,$charset) {$this->hostname =$hostname;$this->username =$username;$this->password =$password;$this->dbname =$dbname;$this->charset =$charset;//Previous Way//Connect to database$this->conn =mysql_connect($this->hostname,$this->username,$this-password);//Select the Operational databasemysql_select_db($this->dbname,$this-conn);//now the way to connect the database and select the Operational database//$this->conn = Mysqli_connect ($this->hostname, $this->username, $this- Password, $this->dbname)//or die (Mysql_error ());//3. Encoding of the set operation$this->query ("Set Names").$this->charset. "'");}//methods for executing SQL statementsfunctionQuery$sql){return mysql_query($sql,$this-conn);}//AddfunctionAdd$table,$set){$sql= "INSERT INTO {$table} set {$set}";$result=$this->query ($sql);return $result;}//ModifyfunctionEdit$table,$set,$where){$sql= "Update {$table} set {$set} WHERE {$where}";$result=$this->query ($sql);return $result;}//DeletefunctionDel$table,$where){$sql= "Delete from {$table} WHERE {$where}";$result=$this->query ($sql);return $result;}//querying a single piece of datafunctionFind$table,$where="",$fields="*",$order=""){$where=Empty($where) ? "": "where".$where;$order=Empty($order) ? "": "Order By".$order;//$limit = Empty ($limit)? "": "Limit". $limit;$sql= "SELECT {$fields} from {$table} {$where} {$order} Limit 1 ";$query=$this->query ($sql);$row=Mysql_fetch_assoc($query);return $row;}//querying more than one piece of datafunctionSelect$table,$where="",$fields="*",$order="",$limit=""){$where=Empty($where) ? "": "where".$where;$order=Empty($order) ? "": "Order By".$order;$limit=Empty($limit) ? "": "Limit".$limit;$sql= "SELECT {$fields} from {$table} {$where} {$order} {$limit} ";$query=$this->query ($sql);$result=Array(); while($row=Mysql_fetch_assoc($query)){//Add the value of $row to the array of $result$result[] =$row;}return $result;}//Recycling Resourcesfunction__destruct () {Mysql_close($this-conn);}}?>file 2:ceshi.PHP<?PHPinclude"Mysql.class.php";$db=New MySQL("localhost", "root", "" "," Test "," GBK ");//perform Add//$db->add ("Test", "id=", uid= ' sky ', Regdate=now (), remark= ' student1 '),//execute Modify//$db->edit ("Test", " Id= ' ", uid= ' Nono ', Regdate=now (), remark= ' Programmer '", "Id= ' 22");//Perform delete//$db->del ("Test", "Id= ' 23");//Perform find//$rs = $ Db->find ("Test", "id=3", "Uid,remark", "desc"),//print_r ($RS);//Perform multiple lookups$rs=$db->select ("Test");Print_r($rs);?>

PHP write MySQL operation class

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.