php连接mysql数据库的类(接口实现)

发布时间:2019-10-12编辑:脚本学堂
本文介绍下,php实现的连接mysql数据库的类,本类先定义一个接口,然后再实现具体方法。有需要的朋友参考下吧。

php与mysql连接类的代码分享,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * @连接mysql数据库的类
 * @filename:Class_Con.inc.php
 
 * @example $this -> Link (); 
 */
 
//接口定义
interface Connected
{
  
// Buat Bayangan Pertama
public function __construct();
  
// Buat Bayangan Function Kedua 
public function connect ();
  
// Buat Bayangan Function Ketiga
public function error_mysql ();
  
// Buat Bayangan Function Keempat
public function db_selected ();
  
// Buat Bayangan Function Kelima 
public function mysql_close ();
}
 
/**
 
 * 使用接品类操作mysql
 
 * @return Function Dari Interface
 * @var String Variable 
 */
 
class ConfigureMysql implements Connected{
  
/**
 * @var String
 */
var $_link ;
  
/**
 * @var String 
 */
var $_Link_Cons ; 
  
/**
 * @var String 
 */
var $_Error; 
  
/**
 * @var String 
 */
var $_DB; 
  
  
// Setting Function Dari Interface
public function __construct() {
  
$this ->_Link_Cons = $this ->connect();
return $this ->_Link_Cons ;
}
  
// Setting Function Kedua Dari Interface 
public function connect () {
  
$this ->_link = @mysql_connect('localhost' , 'Faizal' , 'XXXXXXXXXXX' , '3306') or die($this->error_mysql ());
}
  
// Settiong Function Ketiga Dari Interface 
public function error_mysql () {
  
$this ->_Error = "<h2> Masalah Pada Koneksi Ke Jalur Mysql </h2>";
  
}
  
// Settiong Function Keempat Dari Interface 
public function db_selected () {
  
$this ->_DB = mysql_select_db('XXXXXXXX');
if ($this ->_DB != TRUE) {
return $this ->error_mysql();
}else {
return false ;
}
}
  
// Setting Function Kelima Dari Interface 
public function mysql_close () {
  
return mysql_close($this ->connect());
}
}
 
/**
 * Gunakan Script Classes Untuk Function Parent:: 
 
 * @example parent::__Construct();
 */
 
class LinkCon extends ConfigureMysql  {
  
/**
 * @var String
 */
var $_Con ; 
  
/**
 * @var String
 */
var $_Db ;
  
/**
 * @magic Self::
 */
var  $_Error_Show ;
 
/**
 * @return Mysql_Close 
 */
var $_Close ;
  
  
// Setting Function Dari Class Yang Di Extends
public function Conf_Show_Mysql () {
  
$this ->_Con = parent::__construct();
}
  
// Setting Function Dari Class Yang Di Extends 
public function DB_Selected () {
  
$this ->_Db = $this ->DB_Selected();
return $this ->_Db ;
}
  
// Setting Function Dari Class Yang Di Extends 
public function _CloseMysql () {
  
$this ->_Close = $this ->mysql_close();
return $this ->_Close ;
}
  
// Set Error 
public function Eroor_Show () {
  
$this ->_Error_Show = $this ->error_mysql();;
return true ;
}
  
// Akhir Classes 
}
?>
2,mysql类的调用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// Included File Classes Connected 
include("Class_Con.inc.php");
  
// Set Varibale Untuk Configure Data Classes 
$_Configure = new LinkCon();
  
// Set Variable Function 
$_Configure ->Conf_Show_Mysql();
  
// Set Variable Function 
$_Configure ->DB_Selected();
  
// Set Variable Function 
$_Configure ->_CloseMysql();
  
// Set Variable Function 
$_Configure ->Eroor_Show();
?>
546