remove obsolete frame divider images
[openemr.git] / library / sql.inc
blob20518b0328dbaadc5bea224785eb813a291e14fd
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;
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;
81 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;
91 function sqlLastID() {
92         return mysql_insert_id($GLOBALS['dbh']);
95 function sqlQ ($statement)
97         //echo "[$statement]<br>";
98         //echo "link is: " . $GLOBALS['dbh'];
99         $query = mysql_query($statement, $GLOBALS['dbh']) or 
100         HelpfulDie("query failed: $statement (".mysql_error().")");
101           return $query;
102         return $query;
104 function get_db() {
105         return $GLOBALS['adodb']['db'];
107 function generate_id () {
108         $database = $GLOBALS['adodb']['db'];
109         return $database->GenID("sequences");
112 //fmg: Much more helpful that way...
113 function HelpfulDie ($statement,$sqlerr)
115         echo "<p><p><font color='red'>ERROR:</font> $statement<p>";
116         if ($sqlerr) {
117                 echo "Error: <font color='red'>$sqlerr</font><p>";
118         }//if error
119         exit;