-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
82 lines (64 loc) · 2.72 KB
/
database.php
File metadata and controls
82 lines (64 loc) · 2.72 KB
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
<?php
class Database {
private $link;
public function __construct() {
// Notice that private connection information is *NOT* part of the source
// and therefore does not end up in public repos, etc.
$connectionString = getenv("MYSQLCONNSTR_localdb");
$varsString = str_replace(";","&", $connectionString);
parse_str($varsString);
$host = $Data_Source;
$user = $User_Id;
$pass = $Password;
$db = $Database;
try{
$this->link = new PDO("mysql:host=".$host.";dbname=".$db, $user, $pass);
$this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
echo "Error: Unable to connect to MySQL: ". $e->getMessage();
die;
}
$this->InitializeImageTable();
}
public function __destruct() {
$this->link = null;
}
public function UploadImage($imageName, $imageFP) {
$sql = $this->link->prepare("INSERT INTO images (name, image) VALUES (:name, :image);");
$sql->bindParam(":name", $imageName);
$sql->bindParam(":image", $imageFP, PDO::PARAM_LOB);
$sql->execute();
return $this->link->lastInsertId();
}
public function GetAllImages() {
$sql = $this->link->prepare("SELECT * FROM images;");
$sql->execute();
$results = $sql->fetchAll(PDO::FETCH_OBJ);
return $results;
}
public function FindImage($id) {
$sql = $this->link->prepare("SELECT * FROM images WHERE id = :id;");
$sql->bindParam(":id", $id, PDO::PARAM_INT);
$sql->execute();
$result = $sql->fetch(PDO::FETCH_OBJ);
return $result;
}
private function InitializeImageTable() {
// Check to see if the table needs to be created
$results = $this->link->query("SHOW TABLES LIKE 'images';");
if ($results == TRUE && $results->rowCount() > 0) {
return;
}
// create table
$sql = "CREATE TABLE images (
id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL DEFAULT '',
image LONGBLOB NOT NULL
);";
if ($this->link->query($sql) != TRUE) {
die("Error creating image table: " . $this->link->error);
}
}
}
?>