3 include_once(dirname(__FILE__) . "/sqlconf.php");
4 require_once(dirname(__FILE__) . "/adodb/adodb.inc.php");
6 if (!defined('ADODB_FETCH_ASSOC')) define('ADODB_FETCH_ASSOC', 2);
7 $database = NewADOConnection("mysql");
8 $database->PConnect($host, $login, $pass, $dbase);
10 // Modified 5/2009 by BM for UTF-8 project ---------
11 if (!$disable_utf8_flag) {
12 $success_flag = $database->Execute("SET NAMES 'utf8'");
14 error_log("PHP custom error: from openemr library/sql.inc - Unable to set up UTF8 encoding with mysql database: ".$database->ErrorMsg(), 0);
17 // -------------------------------------------------
19 $GLOBALS['adodb']['db'] = $database;
20 $GLOBALS['dbh'] = $database->_connectionID;
22 //fmg: This makes the login screen informative when no connection can be made
23 if (!$GLOBALS['dbh']) {
24 //try to be more helpful
25 if ($host == "localhost") {
26 echo "Check that mysqld is running.<p>";
28 echo "Check that you can ping the server '$host'.<p>";
30 HelpfulDie("Could not connect to server!", mysql_error($GLOBALS['dbh']));
34 function sqlConnect($login,$pass,$dbase,$host,$port = '3306')
36 $GLOBALS['dbh'] = $database->_connectionID;
37 return $GLOBALS['dbh'];
40 function sqlStatement($statement)
42 //----------run a mysql query, return the handle
43 $query = mysql_query($statement, $GLOBALS['dbh']) or
44 HelpfulDie("query failed: $statement", mysql_error($GLOBALS['dbh']));
48 function idSqlStatement($statement)
50 return sqlInsert($statement);
55 //----------Close our mysql connection
56 $closed = mysql_close($GLOBALS['dbh']) or
57 HelpfulDie("could not disconnect from mysql server link", mysql_error($GLOBALS['dbh']));
61 function sqlInsert($statement)
63 //----------run a mysql insert, return the last id generated
64 mysql_query($statement, $GLOBALS['dbh']) or
65 HelpfulDie("insert failed: $statement", mysql_error($GLOBALS['dbh']));
66 return mysql_insert_id($GLOBALS['dbh']);
69 function sqlInsertClean($statement)
71 return sqlInsert($statement);
74 function sqlFetchArray($resource)
76 if ($resource == FALSE)
78 return mysql_fetch_array($resource, MYSQL_ASSOC);
81 function sqlQuery ($statement)
83 //echo "[$statement]<br>";
84 //echo "link is: " . $GLOBALS['dbh'];
85 $query = mysql_query($statement, $GLOBALS['dbh']) or
86 HelpfulDie("query failed: $statement", mysql_error($GLOBALS['dbh']));
87 $rez = @mysql_fetch_array($query, MYSQL_ASSOC);
93 function sqlListFields($table) {
94 $sql = "SHOW COLUMNS FROM ". mysql_real_escape_string($table);
96 $field_list = array();
97 while($row = mysql_fetch_array($res)) {
98 $field_list[] = $row['Field'];
103 function sqlLastID() {
104 return mysql_insert_id($GLOBALS['dbh']);
107 function sqlQ ($statement)
109 //echo "[$statement]<br>";
110 //echo "link is: " . $GLOBALS['dbh'];
111 $query = mysql_query($statement, $GLOBALS['dbh']) or
112 HelpfulDie("query failed: $statement", mysql_error($GLOBALS['dbh']));
117 return $GLOBALS['adodb']['db'];
120 function generate_id () {
121 $database = $GLOBALS['adodb']['db'];
122 return $database->GenID("sequences");
125 //fmg: Much more helpful that way...
126 function HelpfulDie ($statement, $sqlerr='')
128 echo "<p><p><font color='red'>ERROR:</font> $statement<p>";
130 echo "Error: <font color='red'>$sqlerr</font><p>";