Feature custom loading of dutch prescription class.
[openemr.git] / library / sql.inc
blobaad3d3ee19c071273afb3de9bc6001973f2d69ca
1 <?php
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);
9 $GLOBALS['adodb']['db'] = $database;
10 $GLOBALS['dbh'] = $database->_connectionID;
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
24 function sqlConnect($login,$pass,$dbase,$host,$port = '3306')
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   return sqlInsert($statement);
42 function sqlClose()
44   //----------Close our mysql connection
45   $closed = mysql_close($GLOBALS['dbh']) or
46     HelpfulDie("could not disconnect from mysql server link (" . mysql_error() . ")");
47   return $closed;
50 function sqlInsert($statement)
52   //----------run a mysql insert, return the last id generated
53   mysql_query($statement, $GLOBALS['dbh']) or 
54     HelpfulDie("insert failed: $statement (" . mysql_error() . ")");
55   return mysql_insert_id($GLOBALS['dbh']);
58 function sqlInsertClean($statement)
60   return sqlInsert($statement);
63 function sqlFetchArray($resource)
65   if ($resource == FALSE)
66     return false;
67   return mysql_fetch_array($resource, MYSQL_ASSOC);
70 function sqlQuery ($statement)
72   //echo "[$statement]<br>";
73   //echo "link is: " . $GLOBALS['dbh'];
74   $query = mysql_query($statement, $GLOBALS['dbh']) or 
75     HelpfulDie("query failed: $statement (" . mysql_error() . ")");
76   $rez = @mysql_fetch_array($query, MYSQL_ASSOC);
77   if ($rez == FALSE)
78     return FALSE;
79   return $rez;
82 function sqlListFields($table) {
83   $sql = "SHOW COLUMNS FROM ". mysql_real_escape_string($table);
84   $res = sqlQ($sql);
85   $field_list = array();
86   while($row = mysql_fetch_array($res)) {
87     $field_list[] = $row['Field'];
88   }
89   return $field_list;
92 function sqlLastID() {
93   return mysql_insert_id($GLOBALS['dbh']);
96 function sqlQ ($statement)
98   //echo "[$statement]<br>";
99   //echo "link is: " . $GLOBALS['dbh'];
100   $query = mysql_query($statement, $GLOBALS['dbh']) or
101     HelpfulDie("query failed: $statement (" . mysql_error() . ")");
102   return $query;
105 function get_db() {
106   return $GLOBALS['adodb']['db'];
109 function generate_id () {
110   $database = $GLOBALS['adodb']['db'];
111   return $database->GenID("sequences");
114 //fmg: Much more helpful that way...
115 function HelpfulDie ($statement, $sqlerr='')
117   echo "<p><p><font color='red'>ERROR:</font> $statement<p>";
118   if ($sqlerr) {
119     echo "Error: <font color='red'>$sqlerr</font><p>";
120   }//if error
121   exit;