-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbObject.php
More file actions
85 lines (73 loc) · 2.57 KB
/
dbObject.php
File metadata and controls
85 lines (73 loc) · 2.57 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
83
84
85
<?php
class dbObject {
var $host = 'mysql.appjam.roboteater.com';
var $username = 'icssc_workshop';
var $password = 'macedonia';
var $schema = 'webdev_workshop';
/*
connect connects to a database; the instance variables of the
database class must be initialized
*/
public function connect() {
mysql_connect($this->host,$this->username,$this->password)
or die("Could not connect. " . mysql_error());
mysql_select_db($this->schema)
or die("Could not select database. " . mysql_error());
}
public function getPostById($id) {
//select the post from the database by its id
$sql = "SELECT * FROM blogs WHERE id=$id";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
//create a post object to return
$p = new Post;
$p->id = stripslashes($row['id']);
$p->title = stripslashes($row['title']);
$p->userid = stripslashes($row['userid']);
$p->date = stripslashes($row['date']);
$p->content = stripslashes($row['content']);
return $p;
}
public function getPostByOwner($owner, $num) {
//select the posts
$sql = "SELECT * FROM blogs WHERE userid=$owner ORDER BY date DESC LIMIT $num";
$result = mysql_query($sql);
$pArray = array();
//create a posts object to return
while($row = mysql_fetch_array($result)) {
$p = new Post;
$p->id = stripslashes($row['id']);
$p->title = stripslashes($row['title']);
$p->userid = stripslashes($row['userid']);
$p->date = stripslashes($row['date']);
$p->content = stripslashes($row['content']);
array_push($pArray, $p);
}
return $pArray;
}
public function getAllPosts($num) {
//select the posts
$sql = "SELECT * FROM blogs ORDER BY date DESC LIMIT $num";
$result = mysql_query($sql);
$pArray = array();
//create a posts object to return
while($row = mysql_fetch_array($result)) {
$p = new Post;
$p->id = stripslashes($row['id']);
$p->title = stripslashes($row['title']);
$p->userid = stripslashes($row['userid']);
$p->date = stripslashes($row['date']);
$p->content = stripslashes($row['content']);
array_push($pArray, $p);
}
return $pArray;
}
}
class Post {
var $id;
var $title;
var $userid;
var $date;
var $content;
}
?>