-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbclass.php
148 lines (127 loc) · 3.23 KB
/
dbclass.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
function connect(){
$url=parse_url(getenv("CLEARDB_DATABASE_URL"));
$server = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$db = substr($url["path"],1);
if ($url["host"]) {
$con=mysql_connect($server, $username, $password);
}
else {
$con=mysql_connect("localhost","root","root");
$db="dvh4";
}
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db,$con);
return $con;
}
//check user and pw against database
//should be hashed!!!!!!!!
function authenticateUser(){
$con=connect();
$sql="SELECT * FROM Users
WHERE UserName = '$_POST[firstname]'";
$result = mysql_query($sql,$con);
$row=mysql_fetch_array($result);
if (!$row) return false;//user doesn't exist
mysql_close($con);
return $row['Password']==$_POST['pwd'];
}
//add user if username not taken
function addUser(){
$con=connect();
$sql="SELECT * FROM Users
WHERE UserName = '$_POST[firstname]'";
$result = mysql_query($sql,$con);
$row=mysql_fetch_array($result);
if ($row) return false;
$sql="SELECT * FROM Users
WHERE UserName = '$_POST[firstname]'";
$result = mysql_query($sql,$con);
$sql="INSERT INTO Users
VALUES ('$_POST[firstname]','$_POST[pwd]',NOW(),NOW(),0,0)";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
mysql_close($con);
return true;
}
//updates visits and date if user exists
function updateUser(){
$con=connect();
$sql="SELECT * FROM Users
WHERE UserName = '$_POST[firstname]'";
$result = mysql_query($sql,$con);
$row=mysql_fetch_array($result);
//$row=usernameQuery();
if (!$row){//legit?
echo "user doesn't exist!";
return false;
}
$numVisits=$row['NumVisits']+1;
$sql="UPDATE Users SET NumVisits='$numVisits'
WHERE UserName = '$_POST[firstname]'";
mysql_query($sql,$con);
$sql="UPDATE Users SET LastVisitDate=NOW()
WHERE UserName = '$_POST[firstname]'";
mysql_query($sql,$con);
mysql_close($con);
return true;
}
function getNumPosts(){
$con=connect();
$sql="SELECT * FROM BlogPosts";
$result=mysql_query($sql,$con);
$numRows=mysql_num_rows($result);
mysql_close($con);
return $numRows;
}
function getPost($BlogID){
$con=connect();
$sql="SELECT * FROM BlogPosts
WHERE ID = '$BlogID'";
$result = mysql_query($sql,$con);
$row=mysql_fetch_array($result);
mysql_close($con);
return $row;
}
function getComments($BlogID, $getRows=true){
$con=connect();
$sql="SELECT * FROM Comments
WHERE PostID='$BlogID'";
$result=mysql_query($sql,$con);
if (!$getRows) return $result;
$i=0;
while($row=mysql_fetch_array($result)){
$comments[$i]=$row;
$i=$i+1;
}
mysql_close($con);
return $comments;
}
function addComment($BlogID, $comment, $user){
$con=connect();
$sql="INSERT INTO Comments
VALUES ('$BlogID','$user','$comment',NOW()) ";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
mysql_close($con);
}
function countComments($BlogID){
$result=getComments($BlogID, false);
return mysql_num_rows($result);
}
function getNumLikes($BlogID){
$con=connect();
$sql="SELECT Likes FROM BlogPosts
WHERE ID='$BlogID'";
$result= mysql_query($sql, $con);
$row= mysql_fetch_array($result);
mysql_close($con);
return $row['Likes'];
}
?>