Fix CRLF
[openemr.git] / library / sql.inc
blobc7e34c5a88f17652dd13e0c19cb285f63e8b2396
1 <?php
3 include_once(dirname(__FILE__) . "/sqlconf.php");
4 require_once(dirname(__FILE__) . "/adodb/adodb.inc.php");
5 include_once(dirname(__FILE__) . "/log.inc");
7 if (!defined('ADODB_FETCH_ASSOC')) define('ADODB_FETCH_ASSOC', 2);
8 $database = NewADOConnection("mysql");
9 $database->PConnect($host, $login, $pass, $dbase);
11 // Modified 5/2009 by BM for UTF-8 project ---------
12 if (!$disable_utf8_flag) {
13  $success_flag = $database->Execute("SET NAMES 'utf8'");
14   if (!$success_flag) {
15    error_log("PHP custom error: from openemr library/sql.inc  - Unable to set up UTF8 encoding with mysql database: ".$database->ErrorMsg(), 0);
16   }
18 // -------------------------------------------------
20 $GLOBALS['adodb']['db'] = $database;
21 $GLOBALS['dbh'] = $database->_connectionID;
23 //fmg: This makes the login screen informative when no connection can be made
24 if (!$GLOBALS['dbh']) {
25   //try to be more helpful
26   if ($host == "localhost") {
27     echo "Check that mysqld is running.<p>";
28   } else {
29     echo "Check that you can ping the server '$host'.<p>";
30   }//if local
31   HelpfulDie("Could not connect to server!", mysql_error($GLOBALS['dbh']));
32   exit;
33 }//if no connection
35 function sqlConnect($login,$pass,$dbase,$host,$port = '3306')
37   $GLOBALS['dbh'] = $database->_connectionID;
38   return $GLOBALS['dbh'];
41 function sqlStatement($statement)
43   //----------run a mysql query, return the handle
44   $query = mysql_query($statement, $GLOBALS['dbh']);
45   if ($query === FALSE) {
46     auditSQLEvent($statement, FALSE);
47     HelpfulDie("query failed: $statement", mysql_error($GLOBALS['dbh']));
48   }
49   auditSQLEvent($statement, TRUE);
50   return $query;
53 function idSqlStatement($statement)
55   return sqlInsert($statement);
58 function sqlClose()
60   //----------Close our mysql connection
61   $closed = mysql_close($GLOBALS['dbh']) or
62     HelpfulDie("could not disconnect from mysql server link", mysql_error($GLOBALS['dbh']));
63   return $closed;
66 function sqlInsert($statement)
68   //----------run a mysql insert, return the last id generated
69   $ret = mysql_query($statement, $GLOBALS['dbh']);
70   // To return the correct mysql_insert_id 
71   $lastid= mysql_insert_id($GLOBALS['dbh']);
73   if ($ret === FALSE) {
74     auditSQLEvent($statement, FALSE);
75     HelpfulDie("insert failed: $statement", mysql_error($GLOBALS['dbh']));
76   }
77   auditSQLEvent($statement, TRUE);
78   return $lastid;
81 // ViSolve: Create a seperate function for audit log calls.
82 // The audit calls are handled through separate function.
83 // Before the audit calls are executed, the mysql_insert_id() is stored in GLOBALS['lastiddao']
84 //  and used in the relevant places, i.e in the mysql adodb driver files.. 
85 // Note:  This GLOBAL variable gets its value only when the audit is enabled. 
86 function sqlInsertClean_audit($statement)
88 // Get the mysql_insert_id() before the execution of the log statement
89 $lastid=mysql_insert_id($GLOBALS['dbh']);;
90 $GLOBALS['lastidado']=$lastid;
91   //----------run a mysql insert, return the last id generated
92   $ret = mysql_query($statement, $GLOBALS['dbh']);
93   if ($ret === FALSE) {
94     HelpfulDie("insert failed: $statement", mysql_error($GLOBALS['dbh']));
95   }
98 function sqlInsertClean($statement)
100   return sqlInsert($statement);
103 function sqlNumRows($resource)
105   if (empty($resource)) return 0;
106   return mysql_num_rows($resource);
109 function sqlFetchArray($resource)
111   if ($resource == FALSE)
112     return false;
113   return mysql_fetch_array($resource, MYSQL_ASSOC);
116 function sqlQuery ($statement,$status=false)
118   //echo "[$statement]<br>";
119   //echo "link is: " . $GLOBALS['dbh'];
120   $query = mysql_query($statement, $GLOBALS['dbh']);
121   if (($query === FALSE) && ($status == false)) {
122     auditSQLEvent($statement, FALSE);
123     HelpfulDie("query failed: $statement", mysql_error($GLOBALS['dbh']));
124   }
125   auditSQLEvent($statement, TRUE);
126   $rez = @mysql_fetch_array($query, MYSQL_ASSOC);
127   if ($rez == FALSE)
128     return FALSE;
129   return $rez;
132 function sqlListFields($table) {
133   $sql = "SHOW COLUMNS FROM ". mysql_real_escape_string($table);
134   $res = sqlQ($sql);
135   $field_list = array();
136   while($row = mysql_fetch_array($res)) {
137     $field_list[] = $row['Field'];
138   }
139   return $field_list;
142 function sqlLastID() {
143   return mysql_insert_id($GLOBALS['dbh']);
146 function sqlQ ($statement)
148   //echo "[$statement]<br>";
149   //echo "link is: " . $GLOBALS['dbh'];
150   $query = mysql_query($statement, $GLOBALS['dbh']) or
151     HelpfulDie("query failed: $statement", mysql_error($GLOBALS['dbh']));
152   return $query;
155 function get_db() {
156   return $GLOBALS['adodb']['db'];
159 function generate_id () {
160   $database = $GLOBALS['adodb']['db'];
161   return $database->GenID("sequences");
164 //fmg: Much more helpful that way...
165 function HelpfulDie ($statement, $sqlerr='')
167   echo "<p><p><font color='red'>ERROR:</font> $statement<p>";
168   if ($sqlerr) {
169     echo "Error: <font color='red'>$sqlerr</font><p>";
170   }//if error
171   exit;