-
Notifications
You must be signed in to change notification settings - Fork 1
/
databaselib.php
62 lines (54 loc) · 1.48 KB
/
databaselib.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
<?php
$username = "picturit"; // temporary for local testing
$password = "swagmaster123";
$db_name = "picturit";
$dbh;
function connectDatabase(){
global $dbh;
global $username;
global $password;
global $db_name;
$conf = 'mysql:host=localhost;dbname='.$db_name.';charset=utf8';
$dbh = new PDO($conf, $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
if(!tableExists("images")) createImagesTable();
}
function getAllImages(){
global $dbh;
$stmt = $dbh->prepare("SELECT * FROM `images`");
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $res;
}
function createImagesTable(){
global $dbh;
$dbh->exec(
"CREATE TABLE `images` (
`name` VARCHAR(16) NOT NULL,
`hash` VARCHAR(32) NOT NULL,
`views` INT(11) DEFAULT 0,
UNIQUE KEY `image_ix` (`name`, `hash`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;");
}
function imageExists($hash){
global $dbh;
$stmt = $dbh->prepare("SELECT * FROM `images` WHERE `hash`=?");
$stmt->execute(array($hash));
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $res;
}
function addImageRow($name, $hash){
global $dbh;
$stmt = $dbh->prepare("INSERT INTO `images` (`name`,`hash`) VALUES(:name, :hash)");
$stmt->execute(array(":name"=>$name, ":hash"=>$hash));
}
function tableExists($id){
global $dbh;
$results = $dbh->query("SHOW TABLES LIKE '$id'");
if($results->rowCount()>0){
return TRUE;
}
return FALSE;
}
?>