2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 define('DB_DUPLICATE_ERROR', 1062);
13 define('SQL_MODE', ''); // STRICT_ALL_TABLES,NO_ZERO_IN_DATE ?
15 $db_last_inserted_id = 0;
17 function set_global_connection($company=-1)
19 global $db, $path_to_root, $db_connections, $SysPrefs;
21 include ($path_to_root . "/config_db.php");
23 $company = user_company();
25 cancel_transaction(); // cancel all aborted transactions (if any)
27 $_SESSION["wa_current_user"]->cur_con = $company;
29 $connection = $db_connections[$company];
31 $db = mysqli_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
32 mysqli_select_db($db, $connection["dbname"]);
33 ///// From mysqli release 5.6.6 the sql_mode is no longer empty as it was prior to
34 ///// this release. Just for safety we make it empty for all 5.6 release and higher.
35 ///// This non empty sql_mode values can interphere with FA, so all is set empty during
37 ///// We are, however, investigating the existing code to be compatible in the future.
38 db_query("SET sql_mode = '".SQL_MODE."'");
44 //DB wrapper functions to change only once for whole application
46 function db_query($sql, $err_msg=null)
48 global $db, $SysPrefs, $sql_queries, $Ajax, $db_connections, $db_last_inserted_id;
50 // set current db prefix
51 $comp = isset($_SESSION["wa_current_user"]->cur_con) ? $_SESSION["wa_current_user"]->cur_con : 0;
52 $cur_prefix = $db_connections[$comp]['tbpref'];
53 $sql = str_replace(TB_PREF, $cur_prefix, $sql);
55 if ($SysPrefs->show_sql)
57 $Ajax->activate('footer_debug');
58 $sql_queries .= "<pre>$sql</pre>\n<hr>";
61 db_profile(); // mysql profiling
63 $retry = MAX_DEADLOCK_RETRY;
65 $result = mysqli_query($db, $sql);
66 if (mysqli_errno($db) == 1213) { // deadlock detected
74 if($SysPrefs->sql_trail) {
75 $db_last_inserted_id = mysqli_insert_id($db); // preserve in case trail insert is done
76 if ($SysPrefs->select_trail || (strstr($sql, 'SELECT') === false)) {
77 mysqli_query($db, "INSERT INTO ".$cur_prefix."sql_trail
78 (`sql`, `result`, `msg`)
79 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
80 ".db_escape($err_msg).")");
84 if ($err_msg != null || $SysPrefs->go_debug) {
85 $exit = $err_msg != null;
86 if (function_exists('xdebug_call_file'))
87 $err_msg = '<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg;
88 check_db_error($err_msg, $sql, $exit);
93 function db_fetch_row($result)
95 $ret = mysqli_fetch_row($result);
96 return ($ret === null ? false : $ret);
99 function db_fetch_assoc($result)
101 $ret = mysqli_fetch_assoc($result);
102 return ($ret === null ? false : $ret);
105 function db_fetch($result)
107 $ret = mysqli_fetch_array($result);
108 return ($ret === null ? false : $ret);
111 function db_seek(&$result,$record)
113 return mysqli_data_seek($result, $record);
116 function db_free_result($result)
119 mysqli_free_result($result);
122 function db_num_rows($result)
124 return mysqli_num_rows($result);
127 function db_num_fields($result)
129 return mysqli_num_fields($result);
132 function db_escape($value = "", $nullify = false)
136 $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
137 $value = html_specials_encode($value);
139 //reset default if second parameter is skipped
140 $nullify = ($nullify === null) ? (false) : ($nullify);
142 //check for null/unset/empty strings
143 if ((!isset($value)) || (is_null($value)) || ($value === "")) {
144 $value = ($nullify) ? ("NULL") : ("''");
146 if (is_string($value)) {
147 $value = "'" . mysqli_real_escape_string($db, $value) . "'";
148 //value is a string and should be quoted;
149 } else if (!is_numeric($value)) {
150 //value is not a string nor numeric
151 display_error("ERROR: incorrect data type send to sql query");
159 function db_error_no()
162 return mysqli_errno($db);
165 function db_error_msg($conn)
167 return mysqli_error($conn);
170 function db_insert_id()
172 global $db_last_inserted_id, $SysPrefs, $db;
174 return $SysPrefs->sql_trail ? $db_last_inserted_id : mysqli_insert_id($db);
177 function db_num_affected_rows()
180 return mysqli_affected_rows($db);
183 function db_field_name($result, $n)
185 $fieldinfo = mysqli_fetch_field_direct($result, $n);
186 return $fieldinfo->name;
189 function db_create_db($connection)
191 $db = mysqli_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
193 if (!mysqli_select_db($db, $connection["dbname"]))
195 $sql = "CREATE DATABASE IF NOT EXISTS `" . $connection["dbname"] . "`";
196 if (!mysqli_query($db, $sql) || !mysqli_select_db($db, $connection["dbname"]))
202 function db_drop_db($connection)
205 if ($connection["tbpref"] == "")
208 $sql = "DROP DATABASE IF EXISTS " . $connection["dbname"] . "";
209 return mysqli_query($db, $sql);
213 $res = db_query("show table status");
214 $all_tables = array();
215 while($row = db_fetch($res))
216 $all_tables[] = $row;
217 // get table structures
218 foreach ($all_tables as $table)
220 if (strpos($table['Name'], $connection["tbpref"]) === 0)
221 db_query("DROP TABLE `".$table['Name'] . "`");
223 //deleting the tables, how??
228 function db_close($dbase = null)
234 return mysqli_close($dbase);
237 function db_extension_exists()
239 return function_exists('mysqli_connect');
242 function db_escape_function($string)
246 return (mysqli_real_escape_string($db, $string));
250 Set mysql client encoding.
251 Default is is encoding used by default language.
253 function db_set_encoding($ui_encoding=null)
255 global $db, $dflt_lang, $installed_languages;
257 if (!isset($ui_encoding))
259 $lang = array_search_value($dflt_lang, $installed_languages, 'code');
260 $ui_encoding = strtoupper($lang['encoding']);
263 if ($mysql_enc = get_mysql_encoding_name($ui_encoding))
264 mysqli_set_charset($db, $mysql_enc);
267 function db_get_charset($db)
269 return mysqli_character_set_name($db);
272 function db_set_charset($db, $charset)
276 return mysqli_set_charset($db, $charset);