-
Notifications
You must be signed in to change notification settings - Fork 1
/
comment.php
88 lines (75 loc) · 2.28 KB
/
comment.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
<?php
/*
This class has the db connection and credentials included inside
submit_Comments() and get_Comments().
*/
class Comment
{
private $commentID;
private $comment;
private $upvotes;
private $downvotes;
private $submittime;
function __construct(){}
function getCommentID(){
return $this->commentID;
}
function getComment(){
return $this->comment;
}
function getDownvotes(){
return $this->downvotes;
}
function getUpvotes(){
return $this->upvotes;
}
function getSubmittime(){
return $this->submittime;
}
// Returns bool and takes a string.
function submit_Comment($comm) {
// add connection credentials
include("dbinfo.inc.php");
// format string
$this->comment = nl2br($comm);
// create connection
$pdo = new \PDO("mysql:host={$host};dbname={$database}", $username, $password);
// For error handling
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// Create PDO statement object
$sth = $pdo->prepare("
INSERT INTO Comment VALUES
(NULL, :comment, 0, 0, NOW())
");
// Execute SQL query, bind parameter as we go.
$sth->execute(array(
':comment' => $this->comment
));
// number of rows affected.
if ($sth->rowCount() == 1)
return true;
else
return false;
}
// Returns an array of comments.
function get_Comments(){
// add connection credentials
include("dbinfo.inc.php");
// create connection
$pdo = new \PDO("mysql:host={$host};dbname={$database}", $username, $password);
// For error handling
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// Create PDO statement object
$sth = $pdo->prepare("
SELECT * FROM Comment
ORDER BY submittime DESC
LIMIT 10
");
// Comment class variables must reflect Table column names or a
// proper array will not be returned.
$sth->execute();
$result = $sth->fetchAll(\PDO::FETCH_CLASS, "Comment");
return $result;
}
}
?>