summaryrefslogtreecommitdiff
path: root/classes/db/pdo.php
blob: 3020dea88a0c5d56063e832d0ae1c1d08252646f (plain)
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
<?php
class Db_PDO implements IDb {
	private $pdo;

	function connect($host, $user, $pass, $db, $port) {
		$connstr = DB_TYPE . ":host=$host;dbname=$db;charset=utf8";

		try {
			$this->pdo = new PDO($connstr, $user, $pass);
		} catch (PDOException $e) {
			die($e->getMessage());
		}

		return $this->pdo;
	}

	function escape_string($s, $strip_tags = true) {
		if ($strip_tags) $s = strip_tags($s);

		$qs = $this->pdo->quote($s);

		return mb_substr($qs, 1, mb_strlen($qs)-2);
	}

	function query($query, $die_on_error = true) {
		try {
			return new Db_Stmt($this->pdo->query($query));
		} catch (PDOException $e) {
			user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING);
		}
	}

	function fetch_assoc($result) {
		try {
			if ($result) {
				return $result->fetch();
			} else {
				return null;
			}
		} catch (PDOException $e) {
			user_error($e->getMessage(), E_USER_WARNING);
		}
	}

	function num_rows($result) {
		try {
			if ($result) {
				return $result->rowCount();
			} else {
				return false;
			}
		} catch (PDOException $e) {
			user_error($e->getMessage(), E_USER_WARNING);
		}
	}

	function fetch_result($result, $row, $param) {
		return $result->fetch_result($row, $param);
	}

	function close() {
		$this->pdo = null;
	}

	function affected_rows($result) {
		try {
			if ($result) {
				return $result->rowCount();
			} else {
				return null;
			}
		} catch (PDOException $e) {
			user_error($e->getMessage(), E_USER_WARNING);
		}
	}

	function last_error() {
		return join(" ", $pdo->errorInfo());
	}
}
?>