]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | class Db_Mysql implements IDb { | |
3 | private $link; | |
4 | private $last_error; | |
5 | ||
6 | function connect($host, $user, $pass, $db, $port) { | |
7 | $this->link = mysql_connect($host, $user, $pass); | |
8 | ||
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 | } | |
14 | ||
15 | $this->init(); | |
16 | ||
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) { | |
24 | if ($strip_tags) $s = strip_tags($s); | |
25 | ||
26 | return mysql_real_escape_string($s, $this->link); | |
27 | } | |
28 | ||
29 | function query($query, $die_on_error = true) { | |
30 | $result = @mysql_query($query, $this->link); | |
31 | if (!$result) { | |
32 | $this->last_error = @mysql_error($this->link); | |
33 | ||
34 | @mysql_query("ROLLBACK", $this->link); | |
35 | user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"), | |
36 | $die_on_error ? E_USER_ERROR : E_USER_WARNING); | |
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() { | |
63 | return mysql_error(); | |
64 | } | |
65 | ||
66 | function last_query_error() { | |
67 | return $this->last_error; | |
68 | } | |
69 | ||
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 | ||
80 | } | |
81 | ?> |