forked from paxed/rash-qdb-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
executable file
·52 lines (48 loc) · 1.15 KB
/
db.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
<?php
function get_db($CONFIG, $TEMPLATE=NULL)
{
try {
$db = new PDO($CONFIG['phptype'].":host=localhost;dbname=".$CONFIG['database'], $CONFIG['username'], $CONFIG['password']);
return $db;
} catch (PDOException $dberror) {
if ($TEMPLATE) $TEMPLATE->printheader('Error');
print $dberror->getMessage();
if ($TEMPLATE) $TEMPLATE->printfooter();
exit;
}
}
function check_db_res($res, $query=NULL)
{
global $db;
if (!$res) {
if ($query) print '<p>Query: '.$query.'<p>';
$err = $db->errorInfo();
print '<p>SQLSTATE: '. $err[0];
print '<p>Driver error code: '. $err[1];
print '<p>'.$err[2];
die();
}
}
function db_query($sql)
{
global $db;
$args = array();
for ($i = 1; $i < func_num_args(); $i++)
$args[] = func_get_arg($i);
$sth = $db->prepare($sql);
check_db_res($sth, $sql);
$sth->execute($args);
return $sth;
}
function db_query_singlevalue($sql)
{
global $db;
$args = array();
for ($i = 1; $i < func_num_args(); $i++)
$args[] = func_get_arg($i);
$sth = $db->prepare($sql);
check_db_res($sth, $sql);
$sth->execute($args);
$tmp = $sth->fetch(PDO::FETCH_NUM);
return $tmp[0];
}