added appointments-encounters report
[openemr.git] / library / sql.inc
blob71bc1b77488576d96c2a2b1fb9712f2ca66ca811
1 <?php
3 include_once(dirname(__FILE__) . "/sqlconf.php");
4 require_once(dirname(__FILE__) . "/adodb/adodb.inc.php");
6 define('ADODB_FETCH_ASSOC',2);
7 $database = NewADOConnection("mysql");
8 $database->PConnect($host, $login, $pass, $dbase);
9 $GLOBALS['adodb']['db'] = $database;
10 $GLOBALS['dbh'] = $database->_connectionID;                                     
11                                                                                 
12 //fmg: This makes the login screen informative when no connection can be made...
13 if (!$GLOBALS['dbh']) {                                                         
14         //try to be more helpful                                                
15         if ($host == "localhost") {                                             
16                 echo "Check that mysqld is running.<p>";                        
17         } else {                                                                
18                 echo "Check that you can ping the server '$host'.<p>";          
19         }//if local                                                             
20         HelpfulDie("Could not connect to server!",mysql_error());               
21         exit;                                                                   
22 }//if no connection                                                             
23                                                                                 
24 function sqlConnect($login,$pass,$dbase,$host,$port = '3306')                   
25 {                                                                               
26         return $GLOBALS['dbh'] = $database->_connectionID;
29 function sqlStatement($statement)
31         //----------run a mysql query, return the handle
32         $query = mysql_query($statement, $GLOBALS['dbh']) or 
33         HelpfulDie("query failed: $statement (".mysql_error().")");
34         return $query;
37 function idSqlStatement($statement)
39         //----------run a mysql query, return the handle
40         $query = mysql_query($statement, $GLOBALS['dbh']) or
41         HelpfulDie("query failed: $statement (".mysql_error().")");
42         return mysql_insert_id();
46 function sqlClose()
48         //----------Close our mysql connection
49         $closed = mysql_close($GLOBALS['dbh']) or
50         HelpfulDie("could not disconnect from mysql server link (".mysql_error().")");
51         return $closed;
54 function sqlInsert($statement)
56         //----------run a mysql insert, return the last id generated
57         mysql_query($statement, $GLOBALS['dbh']) or 
58         HelpfulDie("insert failed: $statement (".mysql_error().")");
59         return mysql_insert_id($GLOBALS['dbh']);
62 function sqlInsertClean($statement)
64         //----------run a mysql insert, return the last id generated
65         mysql_query($statement, $GLOBALS['dbh']) or 
66         HelpfulDie("insert failed: $statement (".mysql_error().")");
67         return mysql_insert_id($GLOBALS['dbh']);
70 function sqlFetchArray($resource)
72         if ($resource == FALSE)
73           return false;
74         return mysql_fetch_array($resource, MYSQL_ASSOC);
77 function sqlQuery ($statement)
79         //echo "[$statement]<br>";
80         //echo "link is: " . $GLOBALS['dbh'];
81         $query = mysql_query($statement, $GLOBALS['dbh']) or 
82         HelpfulDie("query failed: $statement (".mysql_error().")");
83         $rez = @mysql_fetch_array($query, MYSQL_ASSOC);
84         if ($rez == FALSE)
85           return FALSE;
86         return $rez;
88 function sqlListFields($table) {
90         $sql = "SHOW COLUMNS FROM ". mysql_real_escape_string($table);
91         $res = sqlQ($sql);
92         $field_list = array();
93         while($row = mysql_fetch_array($res)) {
94                 $field_list[] = $row['Field'];
95         }
96         return $field_list;
98 function sqlLastID() {
99         return mysql_insert_id($GLOBALS['dbh']);
102 function sqlQ ($statement)
104         //echo "[$statement]<br>";
105         //echo "link is: " . $GLOBALS['dbh'];
106         $query = mysql_query($statement, $GLOBALS['dbh']) or 
107         HelpfulDie("query failed: $statement (".mysql_error().")");
108           return $query;
109         return $query;
111 function get_db() {
112         return $GLOBALS['adodb']['db'];
114 function generate_id () {
115         $database = $GLOBALS['adodb']['db'];
116         return $database->GenID("sequences");
118                                                                    
119 //fmg: Much more helpful that way...                               
120 function HelpfulDie ($statement,$sqlerr)                           
121 {                                                                  
122         echo "<p><p><font color='red'>ERROR:</font> $statement<p>";
123         if ($sqlerr) {                                             
124                 echo "Error: <font color='red'>$sqlerr</font><p>"; 
125         }//if error                                                
126         exit;                                                      
127 }