This repository has been archived by the owner on Dec 19, 2020. It is now read-only.
forked from alex-LE/yourTinyTodo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.db.mysql.php
168 lines (137 loc) · 2.87 KB
/
class.db.mysql.php
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
157
158
159
160
161
162
163
164
165
166
167
168
<?php
# Copyright 2009 Max Pozdeev
class DatabaseResult_Mysql
{
var $parent;
var $q;
var $query;
var $rows = NULL;
var $affected = NULL;
var $prefix = '';
function __construct($query, &$h, $resultless = 0)
{
$this->parent = $h;
$this->query = $query;
$this->q = mysql_query($query, $this->parent->dbh);
if(!$this->q)
{
throw new Exception($this->parent->error());
}
}
function affected()
{
if(is_null($this->affected))
{
$this->affected = mysql_affected_rows($this->parent->dbh);
}
return $this->affected;
}
function fetch_row()
{
return mysql_fetch_row($this->q);
}
function fetch_assoc()
{
return mysql_fetch_assoc($this->q);
}
function rows()
{
if (!is_null($this -> rows)) return $this->rows;
$this->rows = mysql_num_rows($this->q);
return $this->rows;
}
}
class Database_Mysql
{
var $dbh;
var $error_str;
function __construct()
{
}
function connect($host, $user, $pass, $db)
{
if(!$this->dbh = @mysql_connect($host,$user,$pass))
{
throw new Exception(mysql_error());
}
if( @!mysql_select_db($db, $this->dbh) )
{
throw new Exception($this->error());
}
return true;
}
function last_insert_id()
{
return mysql_insert_id($this->dbh);
}
function error()
{
return mysql_error($this->dbh);
}
function sq($query, $p = NULL)
{
$q = $this->_dq($query, $p);
if($q->rows()) $res = $q->fetch_row();
else return NULL;
if(sizeof($res) > 1) return $res;
else return $res[0];
}
function sqa($query, $p = NULL)
{
$q = $this->_dq($query, $p);
if($q->rows()) $res = $q->fetch_assoc();
else return NULL;
if(sizeof($res) > 1) return $res;
else return $res[0];
}
function dq($query, $p = NULL)
{
return $this->_dq($query, $p);
}
function ex($query, $p = NULL)
{
return $this->_dq($query, $p, 1);
}
private function _dq($query, $p = NULL, $resultless = 0)
{
if(!isset($p)) $p = array();
elseif(!is_array($p)) $p = array($p);
$m = explode('?', $query);
if(sizeof($p)>0)
{
if(sizeof($m)< sizeof($p)+1) {
throw new Exception("params to set MORE than query params");
}
if(sizeof($m)> sizeof($p)+1) {
throw new Exception("params to set LESS than query params");
}
$query = "";
for($i=0; $i<sizeof($m)-1; $i++) {
$query .= $m[$i]. (is_null($p[$i]) ? 'NULL' : $this->quote($p[$i]));
}
$query .= $m[$i];
}
return new DatabaseResult_Mysql($query, $this, $resultless);
}
function affected()
{
return mysql_affected_rows($this->dbh);
}
function quote($s)
{
return '\''. addslashes($s). '\'';
}
function quoteForLike($format, $s)
{
$s = str_replace(array('%','_'), array('\%','\_'), addslashes($s));
return '\''. sprintf($format, $s). '\'';
}
function table_exists($table)
{
$table = addslashes($table);
$q = mysql_query("SELECT 1 FROM `$table` WHERE 1=0", $this->dbh);
if($q === false) return false;
else return true;
}
}
?>