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'");
15 error_log("PHP custom error: from openemr library/sql.inc - Unable to set up UTF8 encoding with mysql database: ".$database->ErrorMsg(), 0);
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>";
29 echo "Check that you can ping the server '$host'.<p>";
31 HelpfulDie("Could not connect to server!", mysql_error($GLOBALS['dbh']));
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']));
49 auditSQLEvent($statement, TRUE);
53 function idSqlStatement($statement)
55 return sqlInsert($statement);
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']));
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']);
74 auditSQLEvent($statement, FALSE);
75 HelpfulDie("insert failed: $statement", mysql_error($GLOBALS['dbh']));
77 auditSQLEvent($statement, TRUE);
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']);
94 HelpfulDie("insert failed: $statement", mysql_error($GLOBALS['dbh']));
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)
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']));
125 auditSQLEvent($statement, TRUE);
126 $rez = @mysql_fetch_array($query, MYSQL_ASSOC);
132 function sqlListFields($table) {
133 $sql = "SHOW COLUMNS FROM ". mysql_real_escape_string($table);
135 $field_list = array();
136 while($row = mysql_fetch_array($res)) {
137 $field_list[] = $row['Field'];
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']));
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>";
169 echo "Error: <font color='red'>$sqlerr</font><p>";