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 ***********************************************************************/
13 function set_global_connection($company=-1)
15 global $db, $transaction_level, $db_connections;
17 cancel_transaction(); // cancel all aborted transactions if any
18 $transaction_level = 0;
21 $company = $_SESSION["wa_current_user"]->company;
23 $_SESSION["wa_current_user"]->cur_con = $company;
25 $connection = $db_connections[$company];
27 $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
28 mysql_select_db($connection["dbname"], $db);
32 $db_duplicate_error_code = 1062;
34 //DB wrapper functions to change only once for whole application
36 function db_query($sql, $err_msg=null)
38 global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
41 // set current db prefix
42 $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
43 $sql = str_replace(TB_PREF, $cur_prefix, $sql);
47 $Ajax->activate('footer_debug');
48 $sql_queries .= "<pre>$sql</pre>\n<hr>";
51 $result = mysql_query($sql, $db);
53 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
55 "INSERT INTO ".$cur_prefix."sql_trail
56 (`sql`, `result`, `msg`)
57 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
58 ".db_escape($err_msg).")", $db);
62 if ($err_msg != null || $go_debug) {
63 $exit = $err_msg != null;
64 if (function_exists('xdebug_call_file'))
65 check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql, $exit);
67 check_db_error($err_msg, $sql, $exit);
72 function db_fetch_row ($result)
75 return mysql_fetch_row($result);
78 function db_fetch_assoc ($result)
81 return mysql_fetch_assoc($result);
84 function db_fetch ($result)
87 return mysql_fetch_array($result);
90 function db_seek (&$result,$record)
92 return mysql_data_seek($result, $record);
95 function db_free_result ($result)
98 mysql_free_result($result);
101 function db_num_rows ($result)
103 return mysql_num_rows($result);
106 function db_num_fields ($result)
108 return mysql_num_fields($result);
111 function db_escape($value = "", $nullify = false)
113 $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
114 $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
116 //reset default if second parameter is skipped
117 $nullify = ($nullify === null) ? (false) : ($nullify);
119 //check for null/unset/empty strings
120 if ((!isset($value)) || (is_null($value)) || ($value === "")) {
121 $value = ($nullify) ? ("NULL") : ("''");
123 if (is_string($value)) {
124 //value is a string and should be quoted; determine best method based on available extensions
125 if (function_exists('mysql_real_escape_string')) {
126 $value = "'" . mysql_real_escape_string($value) . "'";
128 $value = "'" . mysql_escape_string($value) . "'";
130 } else if (!is_numeric($value)) {
131 //value is not a string nor numeric
132 display_error("ERROR: incorrect data type send to sql query");
140 function db_error_no ()
143 return mysql_errno($db);
146 function db_error_msg($conn)
148 return mysql_error($conn);
151 function db_insert_id()
154 return mysql_insert_id($db);
157 function db_num_affected_rows()
160 return mysql_affected_rows($db);
163 function db_field_name($result, $n)
165 return mysql_field_name($result, $n);
168 function db_create_db($connection)
170 $db = mysql_connect($connection["host"] ,
171 $connection["dbuser"], $connection["dbpassword"]);
172 if (!mysql_select_db($connection["dbname"], $db))
174 $sql = "CREATE DATABASE " . $connection["dbname"] . "";
175 if (!mysql_query($sql) || !mysql_select_db($connection["dbname"], $db))
181 function db_drop_db($connection)
184 if ($connection["tbpref"] == "")
186 $sql = "DROP DATABASE " . $connection["dbname"] . "";
187 return mysql_query($sql);
191 $res = db_query("show table status");
192 $all_tables = array();
193 while($row = db_fetch($res))
194 $all_tables[] = $row;
195 // get table structures
196 foreach ($all_tables as $table)
198 if (strpos($table['Name'], $connection["tbpref"]) === 0)
199 db_query("DROP TABLE `".$table['Name'] . "`");
201 //deleting the tables, how??
206 function db_close($db = null)
208 return mysql_close($db);