Working on new access system...
[estigi.git] / includes / database.inc
blobef1de9e87ec8ea1eb5d231e3762080886e2d02ce
1 <?php
3 /**
4  * @file
5  *  db_connection
6  * 
7  * @ingroup System
8  */
10 /**
11  * Sets a DB as active
12  * @todo
13  *  Protect password from the global space
14  */
15 function db_set_active() {
17         global $db;
19         if (!$db['conn'] = mysql_connect($db['host'], $db['user'], $db['pwd'])) {
20                         echo 'Could not connect to mysql';
21                 exit;
22         }
24         if (!mysql_select_db($db['db'], $db['conn'])) {
25                 echo 'Could not select database';
26                 exit;
31 /**
32  * Unpacks a db query
33  */
34 function db_unpack(&$data){
36         foreach($data as $key => $k){
37                 $var[$key] = $k;
38         }
40         return $var;
43 /**
44  * Sends a db query and replaces tables with their appropriate names
45  * 
46  * @param $query
47  *  The query to be excecuted
48  * @param $unpack
49  *  If you want the system to unpack the results calling db_unpack
50  * @param $single
51  *  If you are expecting or want to reinforce to only receive one row
52  * @param $pager
53  *  For paged queries, not yet implemented
54  * @todo
55  * Implement pager
56  * @bug
57  *  mysql_affected_rows and mysql_num_rows are not properly working
58  */
59 function db_query($query, $unpack = FALSE, $single = FALSE, $pager = FALSE){
61         global $db;
63         static $queries;
65         if($query == 'DEBUG'){
66                 return $queries;
67         }
69         //Debug
70         if(core_variable_get('debug_info', 'No') == 'Yes'){
71                 if(!isset($queries))
72                         $queries = 0;
73         }
75         $queries++;
77         //Replace the pre
78         $db['query'] = str_replace('{PRE_}', $db['pre'], $query);
79         $db['error'] = FALSE;
81         $q = mysql_query($db['query'], $db['conn']);
83         $db['num_rows'] = @mysql_num_rows($q);
85         $db['error'] = mysql_error();
87         $db['affected_rows'] = mysql_affected_rows();
89         $error_display = core_variable_get('db_errors', 'Database Only');
91         if($db['error']){
92                 if($error_display == 'Screen Only' || $error_display == 'Screen and Database'){
93                         system_warnings($db['error'], 'error');
94                         system_warnings($db['query'], 'error');
95                 }
97                 return FALSE;
99         }
100         elseif($db['affected_rows'] > 0){
101                 if($db['num_rows'] > 0){
102                         while($item =  mysql_fetch_assoc($q)){
103                                 $results[] = $item;
104                         }
105         
106                         if($unpack){
107                                 foreach($results as $result){
108                                         $row[] = db_unpack($result);
109                                 }
110         
111                         if(count($row) == 1 && $single){
112                                 $row = $row[0];
113                         }
114         
115                         return $row;
116         
117                         }
118                         else
119                                 return $results;
120                 }
121                 else{
122                         //in case of non-select queries
123                         return TRUE;
124                 }
125         }
126         else{
127                 return FALSE;
128         }