forked from brainfoolong/nreeda
/
Mysql.class.php
147 lines (134 loc) · 4.13 KB
/
Mysql.class.php
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* This file is part of Choqled PHP Framework and/or part of a BFLDEV Software Product.
* This file is licensed under "GNU General Public License" Version 3 (GPL v3).
* If you find a bug or you want to contribute some code snippets, let me know at http://bfldev.com/nreeda
* Suggestions and ideas are also always helpful.
* @author Roland Eigelsreiter (BrainFooLong)
* @product nReeda - Web-based Open Source RSS/XML/Atom Feed Reader
* @link http://bfldev.com/nreeda
**/
if(!defined("CHOQ")) die();
/**
* Mysql DB Handler
*/
class CHOQ_DB_Mysql extends CHOQ_DB_Sql{
/**
* MySQLi
*
* @var MySQLi
*/
public $mysqli;
/**
* Last Result
*
* @var MySQLi_Result
*/
public $lastResult;
/**
* Check requirements for mysql on the system
*
* @return bool
*/
static function isAvailable(){
return class_exists("MySQLi", false);
}
/**
* Constructor
*
* @param array $connectionData Connection Data parsed by parse_url()
*/
public function __construct(array $connectionData){
if(!self::isAvailable()) error("MySQLi not installed - Update your PHP configuration");
$port = isset($connectionData["port"]) ? (int)$connectionData["port"] : 3306;
$pass = isset($connectionData["pass"]) ? $connectionData["pass"] : NULL;
$db = trim($connectionData["path"], "/ ");
$this->dbname = $db;
$this->user = $connectionData["user"];
$this->password = $pass;
$this->host = $connectionData["host"];
$this->port = $port;
$this->mysqli = new MySQLi($this->host, $this->user, $this->password, $this->dbname, $this->port);
$this->testError();
$this->mysqli->set_charset(str_replace("-", "", CHOQ::$encoding));
}
/**
* Convert a string value for database usage
*
* @param string $value
* @return string
*/
public function toDbString($value){
return $this->mysqli->real_escape_string($value);
}
/**
* Fetch rows of query result as associative array
*
* @param string $query
* @param string $valueAsArrayIndex If not null you need to define a field name of the table
* e.g. your table has {id, name, email} fields, if you set it to 'name' the key of resulting
* array is the value of the field 'name'
* @return array[]
*/
public function fetchAsAssoc($query, $valueAsArrayIndex = null){
$fetch = array();
$this->query($query);
while($row = $this->lastResult->fetch_assoc()){
if($valueAsArrayIndex){
if(!isset($row[$valueAsArrayIndex])) error("Field '$valueAsArrayIndex' does not exist in SQL Result");
$fetch[$row[$valueAsArrayIndex]] = $row;
}else{
$fetch[] = $row;
}
}
return $fetch;
}
/**
* Execute the query
*
* @param string $query
* @return mixed
*/
public function query($query){
$this->lastResult = $this->mysqli->query($query);
$this->testError($query);
$this->logQuery($query);
}
/**
* Test if a error exist, if yes than throw error
*
* @throws CHOQ_Exception
* @param string $query
*/
public function testError($query = null){
if($this->mysqli->connect_error){
error("Mysql Connect Error for DBID '".$this->id."'");
}
if($this->mysqli->error){
# retry query when deadlock error is found
if($this->mysqli->errno == 1213){
$this->query($query);
return;
}
$error = $this->mysqli->error;
if($query) $error .= "\nSQL Query: $query";
error($error);
}
}
/**
* Get last insert Id
*
* @return int
*/
public function getLastInsertId(){
return $this->mysqli->insert_id;
}
/**
* This function returns the number of database rows that were changed or inserted or deleted by the most recently completed SQL statement
*
* @return int
*/
public function getAffectedRows(){
return $this->mysqli->affected_rows;
}
}