-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysql.class.php
More file actions
executable file
·136 lines (122 loc) · 3.18 KB
/
Copy pathMysql.class.php
File metadata and controls
executable file
·136 lines (122 loc) · 3.18 KB
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
<?php
class Mysql{
protected $conn = false; //数据库连接资源
protected $sql; //sql语句
/**
* 构造函数,负责连接服务器、选择数据库、设置字符集等
* @param $config string 配置数组
*/
public function __construct($config = array()){
$host = isset($config['host'])? $config['host'] : 'localhost';
$user = isset($config['user'])? $config['user'] : 'root';
$password = isset($config['password'])? $config['password'] : '';
$dbname = isset($config['dbname'])? $config['dbname'] : '';
$port = isset($config['port'])? $config['port'] : '3306';
$charset = isset($config['charset'])? $config['charset'] : 'utf8';
$this->conn = mysql_connect("$host:$port",$user,$password) or die('数据库连接错误');
mysql_select_db($dbname) or die('数据库选择错误');
$this->setChar($charset);
}
/**
* 设置字符集
* @access private
* @param $charset string 字符集
*/
private function setChar($charest){
$sql = 'set names '.$charest;
$this->query($sql);
}
/**
* 执行sql语句
* @access public
* @param $sql string 查询sql语句
* @return $result,成功返回资源,失败则输出错误信息,并退出
*/
public function query($sql){
$this->sql = $sql;
$result = mysql_query($this->sql,$this->conn);
if (! $result) {
die($this->errno().':'.$this->error().'<br />出错语句为'.$this->sql.'<br />');
}
return $result;
}
/**
* 获取第一条记录的第一个字段
* @access public
* @param $sql string 查询的sql语句
* @return 返回一个该字段的值
*/
public function getOne($sql){
$result = $this->query($sql);
$row = mysql_fetch_row($result);
if ($row) {
return $row[0];
} else {
return false;
}
}
/**
* 获取一条记录
* @access public
* @param $sql 查询的sql语句
* @return array 关联数组
*/
public function getRow($sql){
if ($result = $this->query($sql)) {
$row = mysql_fetch_assoc($result);
return $row;
} else {
return false;
}
}
/**
* 获取所有的记录
* @access public
* @param $sql 执行的sql语句
* @return $list 有所有记录组成的二维数组
*/
public function getAll($sql){
$result = $this->query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)){
$list[] = $row;
}
return $list;
}
/**
* 获取某一列的值
* @access public
* @param $sql string 执行的sql语句
* @return $list array 返回由该列的值构成的一维数组
*/
public function getCol($sql){
$result = $this->query($sql);
$list = array();
while ($row = mysql_fetch_row($result)) {
$list[] = $row[0];
}
return $list;
}
/**
* 获取上一步insert操作产生的id
*/
public function getInsertId(){
return mysql_insert_id($this->conn);
}
/**
* 获取错误号
* @access private
* @return 错误号
*/
public function errno(){
return mysql_errno($this->conn);
}
/**
* 获取错误信息
* @access private
* @return 错误private信息
*/
public function error(){
return mysql_error($this->conn);
}
}