forked from MythTV/mythweb
/
mysqlicompat.php
145 lines (134 loc) · 4.6 KB
/
mysqlicompat.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
<?php
/**
* This file was originally written by Chris Petersen for several different open
* source projects. It is distrubuted under the GNU General Public License.
* I (Chris Petersen) have also granted a special LGPL license for this code to
* several companies I do work for on the condition that these companies will
* release any changes to this back to me and the open source community as GPL,
* thus continuing to improve the open source version of the library. If you
* would like to inquire about the status of this arrangement, please contact
* me personally.
*
* ---
*
* This is a special object intended to allow users to connect to the mysqli
* engine provided that it is available and the database version is recent
* enough. The parent Database class will first connect with the legacy mysql
* engine and then check the database version. If everything checks out, a new
* connection will be opened using this library. This library contains fewer
* feature than the full mysqli library, but attempts better backwards
* compatibility so programs can take advantage of the better integration of
* mysqli but still run on older installations.
*
* @copyright Silicon Mechanics
* @license GPL
*
* @package MythWeb
* @subpackage Database
*
* @uses Database.php
* @uses Database_Query_mysqlicompat.php
*
**/
/**
* Database connection class for mysqli, the improved mysql engine.
**/
class Database_mysqlicompat extends Database {
/**
* Constructor
*
* @param string $db_name Name of the database we're connecting to
* @param string $login Login name to use when connecting
* @param string $password Password to use when connecting
* @param string $server Database server to connect to (Default: localhost)
* @param string $port Port or socket address to connect to
**/
function __construct($db_name, $login, $password, $server='localhost', $port=NULL) {
// Attempt to make sure the extension is loaded
if (!extension_loaded('mysql')) {
// This function is deprecated as of php5, so we see if we can use it
if (function_exists('dl')) {
// Attempt to allow dl to be used
ini_set('enable_dl', true);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
dl('php_mysql.dll');
else
dl('mysql.so');
}
}
// Connect to the database
$this->dbh = @mysqli_connect($server, $login, $password, NULL, $port)
or $this->error("Can't connect to the database server.");
if ($this->dbh) {
@mysqli_select_db($this->dbh, $db_name)
or $this->error("Can't access the database file.");
}
}
/**
* Escapes a string and returns it with added quotes. On top of normal escaping,
* this also escapes ? characters so it's safe to use in other db queries.
*
* @param string $string string to escape
*
* @return string escaped string
**/
function escape($string) {
// Null?
if (is_null($string))
return 'NULL';
// Just a string
return str_replace('?', '\\?', "'".mysqli_real_escape_string($this->dbh, $string)."'");
}
/**
* Changes the regexp-special square brackets used for character class/range
* operations to the "match any single character" operator, '.'. This function
* does not do database-special character escapes.
*
* @param string $string string to escape
*
* @return string escaped string
**/
function escape_regex($string) {
// Null?
if (is_null($string))
return 'NULL';
// Just a string
$escaped_string = str_replace('[', '.', $string);
return str_replace(']', '.', $escaped_string);
}
/**
* Returns an un-executed Database_Query_mysqlicompat object
*
* @param string $query The query string
*
* @return Database_Query_mysqli
**/
function &prepare($query) {
$new_query = new Database_Query_mysqlicompat($this, $query);
return $new_query;
}
/**
* @return string The most recent error string
**/
function _errstr() {
return $this->dbh ? mysqli_error($this->dbh) : mysqli_connect_error();
}
/**
* @return int The most recent error number
**/
function _errno() {
return $this->dbh ? mysqli_errno($this->dbh) : mysqli_connect_errno();
}
/**
* @return string Information about the mysql server
**/
function server_info() {
return mysqli_get_server_info($this->dbh);
}
/**
* @return bool true on success
**/
function close() {
return mysqli_close($this->dbh);
}
}