-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMySQL.php
302 lines (282 loc) · 8.4 KB
/
MySQL.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 9160 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class MySQLCore extends Db
{
public function connect()
{
if (!defined('_PS_DEBUG_SQL_'))
define('_PS_DEBUG_SQL_', false);
if ($this->_link = mysql_connect($this->_server, $this->_user, $this->_password))
{
if (!$this->set_db($this->_database))
die(Tools::displayError('The database selection cannot be made.'));
}
else
die(Tools::displayError('Link to database cannot be established.'));
/* UTF-8 support */
if (!mysql_query('SET NAMES \'utf8\'', $this->_link))
die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
// removed SET GLOBAL SQL_MODE : we can't do that (see PSCFI-1548)
return $this->_link;
}
public function getServerVersion(){
return mysql_get_server_info();
}
/* do not remove, useful for some modules */
public function set_db($db_name) {
return mysql_select_db($db_name, $this->_link);
}
public function disconnect()
{
if ($this->_link)
@mysql_close($this->_link);
$this->_link = false;
}
public function getRow($query, $use_cache = 1)
{
$query .= ' LIMIT 1';
$this->_result = false;
$this->_lastQuery = $query;
if ($use_cache AND _PS_CACHE_ENABLED_)
if ($result = Cache::getInstance()->get($query))
{
$this->_lastCached = true;
return $result;
}
if ($this->_link)
if ($this->_result = mysql_query($query, $this->_link))
{
$this->_lastCached = false;
if (_PS_DEBUG_SQL_)
$this->displayMySQLError($query);
$result = mysql_fetch_assoc($this->_result);
if ($use_cache = 1 AND _PS_CACHE_ENABLED_) {
Cache::getInstance()->setQuery($query, $result);
Cache::getInstance()->checkQuery($query);
}
return $result;
}
if (_PS_DEBUG_SQL_)
$this->displayMySQLError($query);
return false;
}
public function getValue($query, $use_cache = 1)
{
$query .= ' LIMIT 1';
$this->_result = false;
$this->_lastQuery = $query;
if ($use_cache AND _PS_CACHE_ENABLED_)
if ($result = Cache::getInstance()->get($query))
{
$this->_lastCached = true;
return $result;
}
if ($this->_link AND $this->_result = mysql_query($query, $this->_link) AND is_array($tmpArray = mysql_fetch_assoc($this->_result)))
{
$this->_lastCached = false;
$result = array_shift($tmpArray);
if ($use_cache = 1 AND _PS_CACHE_ENABLED_) {
Cache::getInstance()->setQuery($query, $result);
Cache::getInstance()->checkQuery($query);
}
return $result;
}
return false;
}
public function Execute($query, $use_cache = 1)
{
$this->_result = false;
if ($this->_link)
{
$this->_result = mysql_query($query, $this->_link);
if (_PS_DEBUG_SQL_)
$this->displayMySQLError($query);
if ($use_cache AND _PS_CACHE_ENABLED_) {
Cache::getInstance()->checkQuery($query);
}
return $this->_result;
}
if (_PS_DEBUG_SQL_)
$this->displayMySQLError($query);
return false;
}
/**
* ExecuteS return the result of $query as array,
* or as mysqli_result if $array set to false
*
* @param string $query query to execute
* @param boolean $array return an array instead of a mysql_result object
* @param int $use_cache if query has been already executed, use its result
* @return array or result object
*/
public function ExecuteS($query, $array = true, $use_cache = 1)
{
$this->_result = false;
$this->_lastQuery = $query;
if ($use_cache AND _PS_CACHE_ENABLED_)
if ($array AND ($result = Cache::getInstance()->get($query)))
{
$this->_lastCached = true;
return $result;
}
if ($this->_link && $this->_result = mysql_query($query, $this->_link))
{
$this->_lastCached = false;
if (_PS_DEBUG_SQL_)
$this->displayMySQLError($query);
if (!$array)
return $this->_result;
$resultArray = array();
// Only SELECT queries and a few others return a valid resource usable with mysql_fetch_assoc
if ($this->_result !== true)
while ($row = mysql_fetch_assoc($this->_result))
$resultArray[] = $row;
if ($use_cache AND _PS_CACHE_ENABLED_) {
Cache::getInstance()->setQuery($query, $resultArray);
Cache::getInstance()->checkQuery($query);
}
return $resultArray;
}
if (_PS_DEBUG_SQL_)
$this->displayMySQLError($query);
return false;
}
public function nextRow($result = false)
{
return mysql_fetch_assoc($result ? $result : $this->_result);
}
public function delete($table, $where = false, $limit = false, $use_cache = 1)
{
$this->_result = false;
if ($this->_link)
{
$query = 'DELETE FROM `'.bqSQL($table).'`'.($where ? ' WHERE '.$where : '').($limit ? ' LIMIT '.(int)($limit) : '');
$res = mysql_query($query, $this->_link);
if ($use_cache AND _PS_CACHE_ENABLED_)
Cache::getInstance()->deleteQuery($query);
return $res;
}
return false;
}
public function NumRows()
{
if (!$this->_lastCached AND $this->_link AND $this->_result)
{
$nrows = mysql_num_rows($this->_result);
if (_PS_CACHE_ENABLED_)
Cache::getInstance()->setNumRows($this->_lastQuery, $nrows);
return $nrows;
}
elseif (_PS_CACHE_ENABLED_ AND $this->_lastCached)
{
return Cache::getInstance()->getNumRows($this->_lastQuery);
}
}
public function Insert_ID()
{
if ($this->_link)
return mysql_insert_id($this->_link);
return false;
}
public function Affected_Rows()
{
if ($this->_link)
return mysql_affected_rows($this->_link);
return false;
}
protected function q($query, $use_cache = 1)
{
global $webservice_call;
$this->_result = false;
if ($this->_link)
{
$result = mysql_query($query, $this->_link);
if ($webservice_call)
$this->displayMySQLError($query);
if ($use_cache AND _PS_CACHE_ENABLED_)
Cache::getInstance()->checkQuery($query);
return $result;
}
return false;
}
/**
* Returns the text of the error message from previous MySQL operation
*
* @acces public
* @return string error
*/
public function getMsgError($query = false)
{
return mysql_error($this->_link);
}
public function getNumberError()
{
return mysql_errno($this->_link);
}
public function displayMySQLError($query = false)
{
global $webservice_call;
if ($webservice_call && mysql_errno($this->_link))
WebserviceRequest::getInstance()->setError(500, '[SQL Error] '.mysql_error($this->_link).'. Query was : '.$query, 97);
elseif (_PS_DEBUG_SQL_ AND mysql_errno($this->_link) AND !defined('PS_INSTALLATION_IN_PROGRESS'))
{
if ($query)
die(Tools::displayError(mysql_error($this->_link).'<br /><br /><pre>'.$query.'</pre>'));
die(Tools::displayError((mysql_error($this->_link))));
}
}
/**
* tryToConnect return 0 if the connection succeed and the database can be selected.
* @since 1.4.4.0, the parameter $newDbLink (default true) has been added.
*
* @param string $server mysql server name
* @param string $user mysql user
* @param string $pwd mysql user password
* @param string $db mysql database name
* @param boolean $newDbLink if set to true, the function will not create a new link if one already exists.
* @return integer
*/
public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true)
{
if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink))
return 1;
if (!@mysql_select_db($db, $link))
return 2;
@mysql_close($link);
return 0;
}
public static function tryUTF8($server, $user, $pwd)
{
$link = @mysql_connect($server, $user, $pwd);
if (!mysql_query('SET NAMES \'utf8\'', $link))
$ret = false;
else
$ret = true;
@mysql_close($link);
return $ret;
}
}