]>
Commit | Line | Data |
---|---|---|
95947917 AD |
1 | <?php |
2 | class Db_Mysql implements IDb { | |
3 | private $link; | |
977cea14 | 4 | private $last_error; |
95947917 AD |
5 | |
6 | function connect($host, $user, $pass, $db, $port) { | |
7 | $this->link = mysql_connect($host, $user, $pass); | |
eefaa2df | 8 | |
95947917 AD |
9 | if ($this->link) { |
10 | $result = mysql_select_db($db, $this->link); | |
11 | if (!$result) { | |
12 | die("Can't select DB: " . mysql_error($this->link)); | |
13 | } | |
404e2e36 AD |
14 | |
15 | $this->init(); | |
16 | ||
95947917 AD |
17 | return $this->link; |
18 | } else { | |
19 | die("Unable to connect to database (as $user to $host, database $db): " . mysql_error()); | |
20 | } | |
21 | } | |
22 | ||
23 | function escape_string($s, $strip_tags = true) { | |
ae35bb87 AD |
24 | if ($strip_tags) $s = strip_tags($s); |
25 | ||
95947917 AD |
26 | return mysql_real_escape_string($s, $this->link); |
27 | } | |
28 | ||
29 | function query($query, $die_on_error = true) { | |
38f43970 | 30 | $result = @mysql_query($query, $this->link); |
95947917 | 31 | if (!$result) { |
977cea14 | 32 | $this->last_error = @mysql_error($this->link); |
38f43970 AD |
33 | |
34 | @mysql_query("ROLLBACK", $this->link); | |
977cea14 | 35 | user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"), |
aca75cb5 | 36 | $die_on_error ? E_USER_ERROR : E_USER_WARNING); |
95947917 AD |
37 | } |
38 | return $result; | |
39 | } | |
40 | ||
41 | function fetch_assoc($result) { | |
42 | return mysql_fetch_assoc($result); | |
43 | } | |
44 | ||
45 | ||
46 | function num_rows($result) { | |
47 | return mysql_num_rows($result); | |
48 | } | |
49 | ||
50 | function fetch_result($result, $row, $param) { | |
51 | return mysql_result($result, $row, $param); | |
52 | } | |
53 | ||
54 | function close() { | |
55 | return mysql_close($this->link); | |
56 | } | |
57 | ||
58 | function affected_rows($result) { | |
59 | return mysql_affected_rows($this->link); | |
60 | } | |
61 | ||
62 | function last_error() { | |
ae35bb87 | 63 | return mysql_error(); |
95947917 AD |
64 | } |
65 | ||
977cea14 AD |
66 | function last_query_error() { |
67 | return $this->last_error; | |
68 | } | |
69 | ||
ba68b681 AD |
70 | function init() { |
71 | $this->query("SET time_zone = '+0:0'"); | |
72 | ||
73 | if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { | |
74 | $this->query("SET NAMES " . MYSQL_CHARSET); | |
75 | } | |
76 | ||
77 | return true; | |
78 | } | |
79 | ||
95947917 AD |
80 | } |
81 | ?> |