2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: functions_mysql.php,v 1.2 2006/07/06 15:17:22 powles Exp $
11 ***************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 ***************************************************************************/
22 /***************************************************************************
24 * NOTE: this is striped down version from phpBB mysql.php file
25 * modified to work with mysqli
27 ***************************************************************************/
29 if(!defined("SQL_LAYER"))
32 define("SQL_LAYER","mysqli");
44 function __construct($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
47 $this->persistency
= $persistency;
48 $this->user
= $sqluser;
49 $this->password
= $sqlpassword;
50 $this->server
= $sqlserver;
51 $this->dbname
= $database;
53 if($this->persistency
)
55 $this->server
= 'p:'.$this->server
;
58 $this->db_connect_id
= mysqli_connect($this->server
, $this->user
, $this->password
);
59 if($this->db_connect_id
)
63 $this->dbname
= $database;
64 $dbselect = mysqli_select_db($this->db_connect_id
, $this->dbname
);
67 mysqli_close($this->db_connect_id
);
68 $this->db_connect_id
= $dbselect;
71 return $this->db_connect_id
;
75 throw new \
RuntimeException('Connection to mySQL failed!');
84 if($this->db_connect_id
)
86 if($this->query_result
)
88 @mysqli_free_result
($this->query_result
);
90 $result = mysqli_close($this->db_connect_id
);
102 function sql_query($query = "", $transaction = FALSE)
104 // Remove any pre-existing queries
105 unset($this->query_result
);
108 nt_common_add_debug($query);
109 $this->num_queries++
;
110 $this->query_result
= mysqli_query($this->db_connect_id
, $query);
112 if($this->query_result
)
114 return $this->query_result
;
118 return ( $transaction == 'END_TRANSACTION' ) ?
true : false;
122 function sql_select_db($dbname)
124 if($this->db_connect_id
)
126 $result = mysqli_select_db($this->db_connect_id
, $dbname);
131 function sql_reselect_db()
133 if($this->db_connect_id
)
135 $result = mysqli_select_db($this->db_connect_id
, $this->dbname
);
141 // Other query methods
143 function sql_numrows($query_id = 0)
147 $query_id = $this->query_result
;
151 $result = mysqli_num_rows($query_id);
159 function sql_affectedrows()
161 if($this->db_connect_id
)
163 $result = mysqli_affected_rows($this->db_connect_id
);
171 function sql_numfields($query_id = 0)
175 $query_id = $this->query_result
;
179 $result = mysqli_num_fields($query_id);
187 // function sql_fieldname($query_id = 0){}
188 // function sql_fieldtype($offset, $query_id = 0){}
189 function sql_fetchrow($query_id = 0)
193 $query_id = $this->query_result
;
197 return mysqli_fetch_array($query_id);
204 function sql_fetchrowset($query_id = 0)
208 $query_id = $this->query_result
;
212 while($row = mysqli_fetch_array($query_id))
223 // function sql_fetchfield($field, $rownum = -1, $query_id = 0){}
224 // function sql_rowseek($rownum, $query_id = 0){}
225 function sql_nextid(){
226 if($this->db_connect_id
)
228 $result = mysqli_insert_id($this->db_connect_id
);
236 function sql_freeresult($query_id = 0){
239 $query_id = $this->query_result
;
244 @mysqli_free_result
($query_id);
253 function sql_escape_string($str)
255 return mysqli_real_escape_string($this->db_connect_id
, $str);
257 function sql_error($query_id = 0)
259 $result["message"] = mysqli_error($this->db_connect_id
);
260 $result["code"] = mysqli_errno($this->db_connect_id
);
267 class sql_db_string
extends sql_db
270 // Constructor ($connstring format : mysql://user:password@host/dbname)
272 function __construct($connstring, $persistency = true)
275 if ($connstring != '')
277 if (preg_match("#^mysqli?://([^:]+)(?::([^@]*))?@([^\\/]+)/([^/]+)[/]?$#", $connstring, $params))
279 $sqlserver = $params[3];
280 $sqluser = $params[1];
281 $sqlpassword = $params[2];
282 $database = $params[4];
284 $ret = parent
::__construct($sqlserver, $sqluser, $sqlpassword, $database, $persistency);
289 } // class sql_db_string