db_insert_id() returned wrong id when $sql_trail=1. Nasty bug resulting in unexpected...
[fa-stable.git] / includes / db / connect_db.inc
1 <?php
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
13 function set_global_connection($company=-1)
14 {
15         global $db, $transaction_level, $db_connections;
16
17         cancel_transaction(); // cancel all aborted transactions if any
18         $transaction_level = 0;
19
20         if ($company == -1) 
21                 $company = $_SESSION["wa_current_user"]->company;
22
23         $_SESSION["wa_current_user"]->cur_con = $company;
24
25         $connection = $db_connections[$company];
26
27         $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
28                 mysql_select_db($connection["dbname"], $db);
29         return $db;
30 }
31
32 $db_duplicate_error_code = 1062;
33
34 //DB wrapper functions to change only once for whole application
35
36 function db_query($sql, $err_msg=null)
37 {
38         global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
39                 $db_connections, $db_last_inserted_id;
40         
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);
44
45         if ($show_sql)
46         {
47                 $Ajax->activate('footer_debug');
48                 $sql_queries .= "<pre>$sql</pre>\n<hr>";
49         }
50
51         $result = mysql_query($sql, $db);
52         
53         if($sql_trail) {
54                 $db_last_inserted_id = mysql_insert_id($db);    // preserve in case trail insert is done
55                 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
56                         mysql_query(
57                         "INSERT INTO ".$cur_prefix."sql_trail
58                                 (`sql`, `result`, `msg`)
59                                 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
60                                 ".db_escape($err_msg).")", $db);
61                 }
62         }
63
64         if ($err_msg != null || $go_debug) {
65                 $exit = $err_msg != null;
66                 if (function_exists('xdebug_call_file'))
67                         check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql, $exit);
68                 else
69                         check_db_error($err_msg, $sql, $exit);
70         }
71         return $result;
72 }
73
74 function db_fetch_row ($result)
75 {
76
77         return mysql_fetch_row($result);
78 }
79
80 function db_fetch_assoc ($result)
81 {
82
83         return mysql_fetch_assoc($result);
84 }
85
86 function db_fetch ($result)
87 {
88
89         return mysql_fetch_array($result);
90 }
91
92 function db_seek (&$result,$record)
93 {
94         return mysql_data_seek($result, $record);
95 }
96
97 function db_free_result ($result)
98 {
99         if ($result)
100                 mysql_free_result($result);
101 }
102
103 function db_num_rows ($result)
104 {
105         return mysql_num_rows($result);
106 }
107
108 function db_num_fields ($result)
109 {
110         return mysql_num_fields($result);
111 }
112
113 function db_escape($value = "", $nullify = false)
114 {
115         $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
116         $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
117
118         //reset default if second parameter is skipped
119         $nullify = ($nullify === null) ? (false) : ($nullify);
120
121         //check for null/unset/empty strings
122         if ((!isset($value)) || (is_null($value)) || ($value === "")) {
123                 $value = ($nullify) ? ("NULL") : ("''");
124         } else {
125                 if (is_string($value)) {
126                 //value is a string and should be quoted; determine best method based on available extensions
127                         if (function_exists('mysql_real_escape_string')) {
128                                 $value = "'" . mysql_real_escape_string($value) . "'";
129                         } else {
130                           $value = "'" . mysql_escape_string($value) . "'";
131                         }
132                 } else if (!is_numeric($value)) {
133                         //value is not a string nor numeric
134                         display_error("ERROR: incorrect data type send to sql query");
135                         echo '<br><br>';
136                         exit();
137                 }
138         }
139         return $value;
140 }
141
142 function db_error_no ()
143 {
144         global $db;
145         return mysql_errno($db);
146 }
147
148 function db_error_msg($conn)
149 {
150         return mysql_error($conn);
151 }
152
153 function db_insert_id()
154 {
155         global $db_last_inserted_id, $sql_trail, $db;
156
157         return $sql_trail ? $db_last_inserted_id : mysql_insert_id($db);
158 }
159
160 function db_num_affected_rows()
161 {
162         global $db;
163         return mysql_affected_rows($db);
164 }
165
166 function db_field_name($result, $n)
167 {
168         return mysql_field_name($result, $n);
169 }
170
171 function db_create_db($connection)
172 {
173         $db = mysql_connect($connection["host"] ,
174                 $connection["dbuser"], $connection["dbpassword"]);
175         if (!mysql_select_db($connection["dbname"], $db))
176         {
177                 $sql = "CREATE DATABASE " . $connection["dbname"] . "";
178                 if (!mysql_query($sql) || !mysql_select_db($connection["dbname"], $db))
179                         return 0;
180         }
181         return $db;
182 }
183
184 function db_drop_db($connection)
185 {
186
187         if ($connection["tbpref"] == "")
188         {
189                 $sql = "DROP DATABASE " . $connection["dbname"] . "";
190                 return mysql_query($sql);
191         }
192         else
193         {
194         $res = db_query("show table status");
195         $all_tables = array();
196         while($row = db_fetch($res))
197                 $all_tables[] = $row;
198         // get table structures
199                 foreach ($all_tables as $table)
200                 {
201                         if (strpos($table['Name'], $connection["tbpref"]) === 0)
202                                 db_query("DROP TABLE `".$table['Name'] . "`");
203                 }
204                 //deleting the tables, how??
205                 return true;
206         }
207 }
208
209 function db_close($dbase = null)
210 {
211         global $db;
212         
213         if (!$dbase)
214                 $dbase = $db;
215         return mysql_close($dbase);
216 }
217
218 ?>