Display all db_query errors in debug mode.
[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()
14 {
15         global $db;
16
17     if (isset($_SESSION["wa_current_user"]) && $_SESSION["wa_current_user"]->company !='')
18         $db = $_SESSION["wa_current_user"]->get_db_connection();
19     else
20         $db = null;
21 }
22
23 $db_duplicate_error_code = 1062;
24
25 //DB wrapper functions to change only once for whole application
26
27 function db_query($sql, $err_msg=null)
28 {
29         global $db, $show_sql, $sql_trail, $select_trail, $go_debug;
30         
31         if (!$err_msg && $go_debug) 
32                 $err_msg = "Debug mode error";
33         //echo "<br>$sql<br>";
34         if ($show_sql)
35         {
36                 echo "<font face=arial size=2 color=000099><b>SQL..</b></font>";
37                 echo "<pre>";
38                 echo $sql;
39                 echo "</pre>\n";
40         }
41         
42
43         $result = mysql_query($sql, $db);
44         if($sql_trail) {
45                 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
46                         mysql_query(
47                         "INSERT INTO ".TB_PREF."sql_trail
48                                 (`sql`, `result`, `msg`)
49                                 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
50                                 ".db_escape($err_msg).")", $db);
51                 }
52         }
53
54         if ($err_msg != null)
55           if (function_exists('xdebug_call_file'))
56                 check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql);
57           else
58                 check_db_error($err_msg, $sql);
59
60         return $result;
61 }
62
63 function db_fetch_row ($result)
64 {
65
66         return mysql_fetch_row($result);
67 }
68
69 function db_fetch_assoc ($result)
70 {
71
72         return mysql_fetch_assoc($result);
73 }
74
75 function db_fetch ($result)
76 {
77
78         return mysql_fetch_array($result);
79 }
80
81 function db_seek (&$result,$record)
82 {
83         return mysql_data_seek($result, $record);
84 }
85
86 function db_free_result ($result)
87 {
88         if ($result)
89                 mysql_free_result($result);
90 }
91
92 function db_num_rows (&$result)
93 {
94         return mysql_num_rows($result);
95 }
96
97 function db_num_fields ($result)
98 {
99         return mysql_num_fields($result);
100 }
101
102 function db_escape($value = "", $nullify = false)
103 {
104         $value = @htmlspecialchars($value, ENT_COMPAT, $_SESSION['language']->encoding);
105
106         //reset default if second parameter is skipped
107         $nullify = ($nullify === null) ? (false) : ($nullify);
108
109         //check for null/unset/empty strings
110         if ((!isset($value)) || (is_null($value)) || ($value === "")) {
111                 $value = ($nullify) ? ("NULL") : ("''");
112         } else {
113                 if (is_string($value)) {
114                 //value is a string and should be quoted; determine best method based on available extensions
115                         if (function_exists('mysql_real_escape_string')) {
116                                 $value = "'" . mysql_real_escape_string($value) . "'";
117                         } else {
118                           $value = "'" . mysql_escape_string($value) . "'";
119                         }
120                 } else if (!is_numeric($value)) {
121                         //value is not a string nor numeric
122                         display_error("ERROR: incorrect data type send to sql query");
123                         echo '<br><br>';
124                         exit();
125                 }
126         }
127         return $value;
128 }
129
130 function db_error_no ()
131 {
132         global $db;
133         return mysql_errno($db);
134 }
135
136 function db_error_msg($conn)
137 {
138         return mysql_error($conn);
139 }
140
141 function db_insert_id()
142 {
143         global $db;
144         return mysql_insert_id($db);
145 }
146
147 function db_num_affected_rows()
148 {
149         global $db;
150         return mysql_affected_rows($db);
151 }
152
153 ?>