]>
Commit | Line | Data |
---|---|---|
65d0cc64 MB |
1 | <?php |
2 | ||
3 | class Db_Mysql extends Db_Abstract | |
4 | { | |
5 | public function connect($host, $user, $pass, $db) | |
6 | { | |
7 | $link = mysql_connect($host, $user, $pass); | |
8 | if ($link) { | |
9 | $result = mysql_select_db($db, $link); | |
10 | if (!$result) { | |
11 | die("Can't select DB: " . mysql_error($link)); | |
12 | } | |
13 | $this->dbconn = $link; | |
14 | return $link; | |
15 | } else { | |
16 | die("Unable to connect to database (as $user to $host, database $db): " . mysql_error()); | |
17 | } | |
18 | } | |
19 | ||
20 | public function init() | |
21 | { | |
22 | db_query($this->dbconn, "SET time_zone = '+0:0'"); | |
23 | ||
24 | if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { | |
25 | db_query($this->dbconn, "SET NAMES " . MYSQL_CHARSET); | |
26 | } | |
27 | } | |
28 | ||
29 | public function escape_string($s, $strip_tags = true) | |
30 | { | |
31 | if ($strip_tags) $s = strip_tags($s); | |
32 | return mysql_real_escape_string($s); | |
33 | } | |
34 | ||
35 | public function query($query, $die_on_error = true) | |
36 | { | |
37 | $result = mysql_query($query, $this->dbconn); | |
38 | if (!$result) { | |
39 | $query = htmlspecialchars($query); | |
40 | if ($die_on_error) { | |
41 | die("Query <i>$query</i> failed: " . ($this->dbconn ? mysql_error($this->dbconn) : "No connection")); | |
42 | } | |
43 | } | |
44 | return $result; | |
45 | } | |
46 | ||
47 | public function fetch_assoc($result) { | |
48 | return mysql_fetch_assoc($result); | |
49 | } | |
50 | ||
51 | public function num_rows($result) { | |
52 | return mysql_num_rows($result); | |
53 | } | |
54 | ||
55 | public function fetch_result($result, $row, $param) { | |
56 | // I hate incoherent naming of PHP functions | |
57 | return mysql_result($result, $row, $param); | |
58 | } | |
59 | ||
60 | public function close() { | |
61 | return mysql_close($this->dbconn); | |
62 | } | |
63 | ||
64 | public function affected_rows($result) { | |
65 | return mysql_affected_rows($this->dbconn); | |
66 | } | |
67 | ||
68 | public function last_error() { | |
69 | return mysql_error($this->dbconn); | |
70 | } | |
71 | } |