XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
mysqldatabase.php
1<?php
13
14if (!defined('XOOPS_ROOT_PATH')) {
15 exit();
16}
17
21include_once XOOPS_ROOT_PATH.'/class/database/database.php';
22
23if (!defined('MYSQL_CLIENT_FOUND_ROWS')) {
24 define('MYSQL_CLIENT_FOUND_ROWS', 2);
25}
26
27class XoopsMySQLDatabase extends XoopsDatabase
28{
33 public $conn;
34
39 public $mPrepareQuery=null;
40
47 public function connect($selectdb = true)
48 {
49 if (XOOPS_DB_PCONNECT == 1) {
50 $this->conn = @mysql_pconnect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, MYSQL_CLIENT_FOUND_ROWS);
51 } else {
52 $this->conn = @mysql_connect(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, false, MYSQL_CLIENT_FOUND_ROWS);
53 }
54
55 if (!$this->conn) {
56 $this->logger->addQuery('', $this->error(), $this->errno());
57 return false;
58 }
59
60 if (false != $selectdb) {
61 if (!mysql_select_db(XOOPS_DB_NAME)) {
62 $this->logger->addQuery('', $this->error(), $this->errno());
63 return false;
64 }
65 }
66
67 // set sql_mode to '' for backward compatibility
68 if (version_compare(mysql_get_server_info($this->conn), '5.6', '>=')) {
69 mysql_query('SET SESSION sql_mode = \'\'', $this->conn);
70 }
71
72 return true;
73 }
74
84 public function genId($sequence)
85 {
86 return 0; // will use auto_increment
87 }
88
95 public function fetchRow($result)
96 {
97 return @mysql_fetch_row($result);
98 }
99
106 public function fetchArray($result)
107 {
108 return @mysql_fetch_assoc($result);
109 }
110
117 public function fetchBoth($result)
118 {
119 return @mysql_fetch_array($result, MYSQL_BOTH);
120 }
121
127 public function getInsertId()
128 {
129 return mysql_insert_id($this->conn);
130 }
131
138 public function getRowsNum($result)
139 {
140 return @mysql_num_rows($result);
141 }
142
148 public function getAffectedRows()
149 {
150 return mysql_affected_rows($this->conn);
151 }
152
157 public function close()
158 {
159 mysql_close($this->conn);
160 }
161
168 public function freeRecordSet($result)
169 {
170 return mysql_free_result($result);
171 }
172
178 public function error()
179 {
180 return @mysql_error();
181 }
182
188 public function errno()
189 {
190 return @mysql_errno();
191 }
192
199 public function quoteString($str)
200 {
201 $str = '\''.mysql_real_escape_string($str, $this->conn).'\'';
202 return $str;
203 }
204
214 public function &queryF($sql, $limit=0, $start=0)
215 {
216 if (!empty($limit)) {
217 if (empty($start)) {
218 $sql .= ' LIMIT ' . (int)$limit;
219 } else {
220 $sql = $sql. ' LIMIT '.(int)$start.', '.(int)$limit;
221 }
222 }
223 $result = mysql_query($sql, $this->conn);
224 if ($result) {
225 $this->logger->addQuery($sql);
226 return $result;
227 } else {
228 $this->logger->addQuery($sql, $this->error(), $this->errno());
229 $ret = false;
230 return $ret;
231 }
232 }
233
246 public function &query($sql, $limit=0, $start=0)
247 {
248 }
249
257 public function queryFromFile($file)
258 {
259 if (false !== ($fp = fopen($file, 'r'))) {
260 include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php';
261 $sql_queries = trim(fread($fp, filesize($file)));
262 SqlUtility::splitMySqlFile($pieces, $sql_queries);
263 foreach ($pieces as $query) {
264 // [0] contains the prefixed query
265 // [4] contains unprefixed table name
266 $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix());
267 if (false != $prefixed_query) {
268 $this->query($prefixed_query[0]);
269 }
270 }
271 return true;
272 }
273 return false;
274 }
275
283 public function getFieldName($result, $offset)
284 {
285 return mysql_field_name($result, $offset);
286 }
287
295 public function getFieldType($result, $offset)
296 {
297 return mysql_field_type($result, $offset);
298 }
299
306 public function getFieldsNum($result)
307 {
308 return mysql_num_fields($result);
309 }
310
316 public function prepare($query)
317 {
318 $count=0;
319 while (false !== ($pos=strpos($query, '?'))) {
320 $pre=substr($query, 0, $pos);
321 $after='';
322 if ($pos+1<=strlen($query)) {
323 $after=substr($query, $pos+1);
324 }
325
326 $query=$pre.'{'.$count.'}'.$after;
327 $count++;
328 }
329 $this->mPrepareQuery=$query;
330 }
331
336 public function bind_param()
337 {
338 if (func_num_args()<2) {
339 return;
340 }
341
342 $types=func_get_arg(0);
343 $count=strlen($types);
344 if (func_num_args()<$count) {
345 return;
346 }
347
348 $searches= [];
349 $replaces= [];
350 for ($i=0;$i<$count;$i++) {
351 $searches[$i]='{'.$i.'}';
352 switch (substr($types, $i, 1)) {
353 case 'i':
354 $replaces[$i]=(int)func_get_arg($i+1);
355 break;
356
357 case 's':
358 $replaces[$i]=$this->quoteString(func_get_arg($i+1));
359 break;
360
361 case 'd':
362 $replaces[$i]=floatval(func_get_arg($i + 1));
363 break;
364
365 case 'b':
366 // Exception
367 die();
368 }
369 }
370
371 $this->mPrepareQuery=str_replace($searches, $replaces, $this->mPrepareQuery);
372 }
373
378 public function &execute()
379 {
380 $result=&$this->query($this->mPrepareQuery);
381 $this->mPrepareQuery=null;
382 return $result;
383 }
384
389 public function &executeF()
390 {
391 $result=&$this->queryF($this->mPrepareQuery);
392 $this->mPrepareQuery=null;
393 return $result;
394 }
395}
396
408{
409
419 public function &query($sql, $limit=0, $start=0)
420 {
421 $result =& $this->queryF($sql, $limit, $start);
422 return $result;
423 }
424}
425
440{
441
452 public function &query($sql, $limit=0, $start=0)
453 {
454 $sql = ltrim($sql);
455 if (preg_match('/^SELECT/i', $sql)) {
456 $ret = $this->queryF($sql, $limit, $start);
457 return $ret;
458 }
459 $this->logger->addQuery($sql, 'Database update not allowed during processing of a GET request', 0);
460
461 $ret = false;
462 return $ret;
463 }
464}
& query($sql, $limit=0, $start=0)
& queryF($sql, $limit=0, $start=0)
getFieldType($result, $offset)
getFieldName($result, $offset)
connect($selectdb=true)
& query($sql, $limit=0, $start=0)
& query($sql, $limit=0, $start=0)