forked from nruffilo/regexquest.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseUtil.php
executable file
·99 lines (86 loc) · 2.87 KB
/
DatabaseUtil.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Database Utility is used to handle all interactions with the database.
*/
class DatabaseUtil {
private $dbconn; //hold the db connection - should not be accessable outside of this object
/**
* the construct will connect to the database using the connection parameters given in the server_config.php
* @author Nick Ruffilo
*/
public function __construct() {
global $config;
//$this->dbconn =& MDB2::connect($dsn, $options);
$this->dbconn = mysql_pconnect($config['db_server'],$config['db_user'],$config['db_pass'])
or die("could not connect: " . mysql_error());
mysql_select_db($config['db_name']) or die("could not select DB");
}
/**
* Query will run a query against the database. It will run any type of query
*
* @param STRING $sql The SQL that you want run
* @return ARRAY $results An array of resulting rows from your query. If they query was an insert or update, it will return the row inserted id or the # of rows effected
*/
public function query($sql) {
//global $ANALYZE_SQL;
$trimmed_sql = trim($sql);
$res = mysql_query($trimmed_sql) or die("failed: ($trimmed_sql) " . mysql_error());
if ((stripos($trimmed_sql, 'insert') === 0) || (stripos($trimmed_sql, 'replace') === 0)) {
return mysql_insert_id();
}
if (stripos($trimmed_sql, "update") === 0) {
return true;
}
$return = array();
if (count($res)>0) {
while ($item = mysql_fetch_array($res, MYSQL_ASSOC)) {
$ret_item = array();
foreach ($item as $key=>$val) {
$ret_item[$key]=$val;
}
$return[] = $ret_item;
}
}
return $return;
}
/**
* Query will run a query against the database and return ONE result (the first). It will run any type of query
*
* @param STRING $sql The SQL that you want run
* @return ARRAY $results An array of resulting rows from your query. If they query was an insert or update, it will return the row inserted id or the # of rows effected
*/
public function query_one($sql) {
$result = $this->query($sql);
//$line = mysql_fetch_array($result, MYSQL_ASSOC);
if (isset($result[0])) {
return $result[0];
} else {
return array();
}
}
public function parseForIn($array, $elements_are_strings = false) {
if(!is_array($array)) return false;
$string = "(";
foreach ($array as $elem) {
if(!$elements_are_strings) $string .= $elem.", ";
else $string .= "'".$elem."', ";
}
$string = substr_replace($string,'',-2);
$string .= ')';
return $string;
}
/**
* This is for when there are multiple queries being ran, this will free up memory
*
*/
public function clearDebugOutput() {
$this->dbconn->debug_output = '';
}
/**
* Disconnect will close the connection
*/
public function disconnect() {
mysql_close($this->link);
}
}
?>