XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
mysqlidatabase.php
1<?php
12
13if (!defined('XOOPS_ROOT_PATH')) {
14 exit();
15}
16
20include_once XOOPS_ROOT_PATH.'/class/database/database.php';
21
22mysqli_report( MYSQLI_REPORT_ERROR ); // for backword compat
23
24class XoopsMysqliDatabase extends XoopsDatabase
25{
30 public $conn;
31
36 public $mPrepareQuery=null;
37
44 public function connect($selectdb = true)
45 {
46 $this->conn = mysqli_init();
47
48 if (!$this->conn) {
49 $this->logger->addQuery('', $this->error(), $this->errno());
50 return false;
51 }
52
53 if (XOOPS_DB_PCONNECT == 1 && PHP_VERSION_ID >= 50300 ) {
54 mysqli_real_connect($this->conn, 'p:'.XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, null, null, null, MYSQLI_CLIENT_FOUND_ROWS);
55 } else {
56 mysqli_real_connect($this->conn, XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, null, null, null, MYSQLI_CLIENT_FOUND_ROWS);
57 }
58
59 if (false != $selectdb) {
60 if (!mysqli_select_db($this->conn, XOOPS_DB_NAME)) {
61 $this->logger->addQuery('', $this->error(), $this->errno());
62 return false;
63 }
64 }
65
66 // set sql_mode to '' for backward compatibility
67 if (version_compare(mysqli_get_server_info($this->conn), '5.6', '>=')) {
68 mysqli_query($this->conn, 'SET SESSION sql_mode = \'\'');
69 }
70
71 return true;
72 }
73
83 public function genId($sequence)
84 {
85 return 0; // will use auto_increment
86 }
87
94 public function fetchRow($result)
95 {
96 return @ mysqli_fetch_row($result);
97 }
98
105 public function fetchArray($result)
106 {
107 return @ mysqli_fetch_assoc($result);
108 }
109
116 public function fetchBoth($result)
117 {
118 return @ mysqli_fetch_array($result, MYSQLI_BOTH);
119 }
120
126 public function getInsertId()
127 {
128 return mysqli_insert_id($this->conn);
129 }
130
137 public function getRowsNum($result)
138 {
139 return @ mysqli_num_rows($result);
140 }
141
147 public function getAffectedRows()
148 {
149 return mysqli_affected_rows($this->conn);
150 }
151
156 public function close()
157 {
158 mysqli_close($this->conn);
159 }
160
167 public function freeRecordSet($result)
168 {
169 return mysqli_free_result($result);
170 }
171
177 public function error()
178 {
179 return @ mysqli_error($this->conn);
180 }
181
187 public function errno()
188 {
189 return @ mysqli_errno($this->conn);
190 }
191
198 public function quoteString($str)
199 {
200 $str = '\''.mysqli_real_escape_string($this->conn, $str ?? '').'\'';
201 return $str;
202 }
203
213 public function &queryF($sql, $limit=0, $start=0)
214 {
215 if (!empty($limit)) {
216 if (empty($start)) {
217 $sql .= ' LIMIT ' . (int)$limit;
218 } else {
219 $sql = $sql. ' LIMIT '.(int)$start.', '.(int)$limit;
220 }
221 }
222 $result = mysqli_query($this->conn, $sql);
223
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 if ($finfo = mysqli_fetch_field_direct($result, $offset)) {
286 return $finfo->orgname?: $finfo->name;
287 } else {
288 return false;
289 }
290 }
291
299 public function getFieldType($result, $offset)
300 {
301 if ($finfo = mysqli_fetch_field_direct($result, $offset)) {
302 return $finfo->type;
303 } else {
304 return false;
305 }
306 }
307
314 public function getFieldsNum($result)
315 {
316 return mysqli_num_fields($result);
317 }
318
325 public function setCharset($charset)
326 {
327 return mysqli_set_charset($this->conn, $charset);
328 }
329
335 public function prepare($query)
336 {
337 $count=0;
338 while (false !== ($pos=strpos($query, '?'))) {
339 $pre=substr($query, 0, $pos);
340 $after='';
341 if ($pos+1<=strlen($query)) {
342 $after=substr($query, $pos+1);
343 }
344
345 $query=$pre.'{'.$count.'}'.$after;
346 $count++;
347 }
348 $this->mPrepareQuery=$query;
349 }
350
355 public function bind_param()
356 {
357 if (func_num_args()<2) {
358 return;
359 }
360
361 $types=func_get_arg(0);
362 $count=strlen($types);
363 if (func_num_args()<$count) {
364 return;
365 }
366
367 $searches= [];
368 $replaces= [];
369 for ($i=0;$i<$count;$i++) {
370 $searches[$i]='{'.$i.'}';
371 switch (substr($types, $i, 1)) {
372 case 'i':
373 $replaces[$i]=(int)func_get_arg($i+1);
374 break;
375
376 case 's':
377 $replaces[$i]=$this->quoteString(func_get_arg($i+1));
378 break;
379
380 case 'd':
381 $replaces[$i]= (float)func_get_arg($i + 1);
382 break;
383
384 case 'b':
385 // Exception
386 die();
387 }
388 }
389
390 $this->mPrepareQuery=str_replace($searches, $replaces, $this->mPrepareQuery);
391 }
392
397 public function &execute()
398 {
399 $result=&$this->query($this->mPrepareQuery);
400 $this->mPrepareQuery=null;
401 return $result;
402 }
403
408 public function &executeF()
409 {
410 $result=&$this->queryF($this->mPrepareQuery);
411 $this->mPrepareQuery=null;
412 return $result;
413 }
414}
415
427{
428
438 public function &query($sql, $limit=0, $start=0)
439 {
440 $result =& $this->queryF($sql, $limit, $start);
441 return $result;
442 }
443}
444
459{
460
471 public function &query($sql, $limit=0, $start=0)
472 {
473 $sql = ltrim($sql);
474 if (preg_match('/^SELECT/i', $sql)) {
475 $ret = $this->queryF($sql, $limit, $start);
476 return $ret;
477 }
478 $this->logger->addQuery($sql, 'Database update not allowed during processing of a GET request', 0);
479
480 $ret = false;
481 return $ret;
482 }
483}
& 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)