1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework\Database;
10:
11:
12: use DebugBar\DataCollector\PDO\TraceablePDOStatement;
13: use GLFramework\Events;
14: use TijsVerkoyen\CssToInlineStyles\Exception;
15:
16: class MySQLConnection extends Connection
17: {
18:
19: 20: 21:
22: private $link;
23: 24: 25:
26: private $pdo;
27: public function connect($hostname, $username, $password)
28: {
29: try{
30: $this->pdo = new \PDO('mysql:host=' . $hostname . ';', $username, $password);
31: Events::fire('onPDOCreated', array(&$this->pdo));
32: if($this->pdo->errorCode() == 0)
33: {
34: $this->pdo->exec("SET NAMES utf8");
35: return true;
36: }
37: }catch(\Exception $ex)
38: {
39:
40: }
41: return false;
42: }
43:
44: public function select_database($database)
45: {
46:
47:
48: if($this->pdo)
49: {
50: if($this->pdo->exec("USE " . $database) !== FALSE)
51: return true;
52: }
53:
54:
55:
56:
57: return false;
58: }
59:
60: public function escape_string($value)
61: {
62: if($this->pdo)
63: {
64: return substr($this->pdo->quote($value), 1, -1);
65: }
66: return $value;
67: }
68:
69: public function select($query, $returnArray = true)
70: {
71: if(!GL_TESTING && !GL_INSTALL)
72: {
73: $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
74:
75: }
76: else{
77: $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
78:
79: }
80: $result = $this->pdo->query($query);
81: $list = array();
82: if ($result) {
83: if($returnArray)
84: {
85: return $result->fetchAll();
86: }
87: return true;
88: } else {
89: throw new \Exception($query . "\n" . $this->getLastError());
90: }
91: }
92:
93: public function getLastInsertId()
94: {
95: return $this->pdo->lastInsertId();
96: }
97:
98: public function getLastError()
99: {
100: $error = $this->pdo->errorInfo();
101: return $error[2];
102: }
103:
104: public function getPDO()
105: {
106: return $this->pdo;
107: }
108: }