Home  >  Article  >  Backend Development  >  PHP development_A class to link to mysql database_PHP tutorial

PHP development_A class to link to mysql database_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:50:251015browse


Project structure:
PHP development_A class to link to mysql database_PHP tutorial
Operation effect;
PHP development_A class to link to mysql database_PHP tutorial



conn.php

1 2 class ConnectionMySQL{
3 //Host
4 private $host="localhost";
5 //Database username
6 private $name="root";
7 //Database password
8 private $pass="";
9      //Database name
10 private $table="phptest";
11 //Encoding form
12 private $ut="utf-8";
13
14
15 //Constructor
16 function __construct(){
17          $this->ut=$ut;
18           $this->connect();
19
20 }
21
22 //Database link
23 function connect(){
24           $link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());
25 mysql_select_db($this->table,$link) or die("No such database:".$this->table);
26 mysql_query("SET NAMES '$this->ut'");
27 }
28
29 function query($sql, $type = '') {
30    if(!($query = mysql_query($sql))) $this->show('Say:', $sql);
31          return $query;
32 }
33
34 function show($message = '', $sql = '') {
35 if(!$sql) echo $message;
36 else echo $message.'
'.$sql;
37 }
38
39 function affected_rows() {
40         return mysql_affected_rows();
41 }
42
43 function result($query, $row) {
44          return mysql_result($query, $row);
45 }
46
47 function num_rows($query) {
48           return @mysql_num_rows($query);
49 }
50
51 function num_fields($query) {
52          return mysql_num_fields($query);
53 }
54
55 function free_result($query) {
56          return mysql_free_result($query);
57 }
58
59 function insert_id() {
60         return mysql_insert_id();
61 }
62
63 function fetch_row($query) {
64          return mysql_fetch_row($query);
65 }
66
67 function version() {
68           return mysql_get_server_info();
69 }
70
71 function close() {
72          return mysql_close();
73 }
74
75 //Insert values ​​into $table www.2cto.com
76 function fn_insert($table,$name,$value){
77            $this->query("insert into $table ($name) value ($value)");
78 }
79 //Delete a record in table $table based on $id value
80 function fn_delete($table,$id,$value){
81           $this->query("delete from $table where $id=$value");
82             echo "The record with id ".$id." was successfully deleted!";
83 }
84}
85
86 $db = new ConnectionMySQL();
87
88 $db->fn_insert('test','id,name,sex',"'','hongtenzone','M'");
89 $db->fn_delete('test', 'id', 1);
90
91 ?>

Excerpted from Hongten

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478281.htmlTechArticleProject structure: Running effect; conn.php 1 ?php 2 class ConnectionMySQL{ 3 //Host 4 private $host =localhost; 5 //Database username 6 private $name=root; 7 //Database password...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn