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を利用する場合のインターフェース
* @author zhouz
* @access public
* @package jp.aimslib2.database.mysql
*/
class MySQLDatabaseAdaptor {
public $host;
public $port;
public $db_name;
public $user;
public $pass;
public $con = null;
public $result;
public $lastquery;
/**
* コンストラクタ
*/
function constructor(){
}
/**
* 初期化
*/
function init($host, $port, $db_name, $user, $pass){
$this->host = $host;
if(isset($port)){
$this->port = $port;
}else{
$this->port = "3306";
}
$this->db_name = $db_name;
$this->user = $user;
$this->pass = $pass;
}
/**
* 接続の確立
*/
public function db_connect(){
if (!is_resource($this->con)) {
$this->con = @mysqli_connect($this->host, $this->user, $this->pass, "", $this->port, "");
// 文字コード変換指定が必要な場合
if (defined("MYSQL_USE_CLIENT_CHARSET") && MYSQL_USE_CLIENT_CHARSET != null) {
if (function_exists('mysqli_set_charset') === false) {
ErrorLogger::doOutput("using set names to:" . MYSQL_USE_CLIENT_CHARSET, 0, "MySQL");
// PHP4ではこっちを使う必要がある
mysqli_query("SET NAMES " . MYSQL_USE_CLIENT_CHARSET, $this->con);
} else {
ErrorLogger::doOutput("using mysqli_set_charset to:" . MYSQL_USE_CLIENT_CHARSET, 0, "MySQL");
mysqli_set_charset(MYSQL_USE_CLIENT_CHARSET, $this->con);
}
}
mysqli_select_db($this->con, $this->db_name);
}
}
/**
* 接続の解除(使わない)
*/
public function db_close() {
mysqli_close($this->con);
}
/**
* Queryを実行する。
* 結果は$resultにはいる。
*/
public function db_exec($query) {
$this->db_connect();
$this->lastquery = $query;
ErrorLogger::doOutput($query, 0, "SQL");
//echo $query;
if(@$this->result = mysqli_query($this->con, $query)){
$rowset = $this->private_getRowset($this->result);
@mysqli_free_result($this->result);
return $rowset;
} else {
ErrorLogger::doOutput(mysqli_error($this->con), 0);
ErrorLogger::doOutput("error at:" . $this->lastquery, 0);
}
$this->db_close();
return "QUERY_ERROR";
}
/**
* 結果を2次元連想配列にして返します。
* @access private
* @param resource $result 検索の結果リソース
* @return array カラム名をkeyとした連想配列を要素として持つ配列
*/
public function private_getRowset(&$result){
$rowset = array();
if (empty($result)) {
return null;
}
//数值类型
if (is_numeric($result) || ($result===true) || ($result===false)) {
return null;
}
$string_field = array();
$bool_field = array();
$fields = mysqli_fetch_fields($result);
for ($x = 0; $x < count($fields); $x++ ) {
$field = $fields[$x];
$name = $field->name;
//ErrorLogger::doOutput("MYSQL TYPE:" . $name . " - " . $field->type . " - " . $field->length, 0);
if ($field->type == 1) {
// boolean
array_push($bool_field, $name);
} else {
array_push($string_field, $name);
}
}
while ($row = mysqli_fetch_assoc($result)) {
foreach ($string_field as $name) {
$row[$name] = SjisUtil::stripMysqlSlashesLike5C($row[$name]);
}
foreach ($bool_field as $name) {
if ($row[$name] == 1) {
$row[$name] = true;
} else {
$row[$name] = false;
}
}
array_push($rowset, $row);
}
return $rowset;
}
}
?>