-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbmanage.php
executable file
·114 lines (101 loc) · 3.25 KB
/
dbmanage.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
<?php
//データベース操作クラス
class dbmanage
{
// post用メソッド
public function post($userid, $text) {
$dsn = 'mysql:host=localhost;dbname=board2;';
$user ='root';
$password = 'root';
try {
$db = new PDO($dsn,$user,$password);
$stmt = $db->prepare('INSERT INTO post(contents,user_id)VALUES(:contents,:user_id)');
$stmt->bindParam(':user_id', $userid, PDO::PARAM_STR);
$stmt->bindParam(':contents', $text, PDO::PARAM_STR);
// クエリの実行
$stmt->execute();
return "ok";
}
catch (PDOException $e) {
die ('エラー:' . $e->getMessage());
}
}
// 全投稿内容取得用メソッド
public function post_list(){
$dsn = 'mysql:host=localhost;dbname=board2;';
$user = 'root';
$password = 'root';
try{
$db = new PDO($dsn,$user,$password);
$stt = $db->query('SELECT * FROM post ORDER BY id DESC');
$stt->execute();
$post_list = $stt->fetchAll();
if ($post_list !=null){
return $post_list;
}
else{
return null;
}
}
catch(PDOException $e) {
die("エラーメッセージ:{$e->getMessage()}");
}
}
// 投稿内容取得用メソッド
public function post_contents($content_id){
$dsn = 'mysql:host=localhost;dbname=board2;';
$user = 'root';
$password = 'root';
$id = $content_id;
try{
$db = new PDO($dsn,$user,$password);
$sql = "SELECT * FROM post WHERE id = $id";
$stt = $db->query($sql);
$post_contents = $stt->fetchAll();
if ($post_contents !=null){
return $post_contents;
}
else{
return null;
}
}
catch(PDOException $e) {
die("エラーメッセージ:{$e->getMessage()}");
}
}
// 投稿更新用メソッド
public function update($id, $text) {
$dsn = 'mysql:host=localhost;dbname=board2;';
$user ='root';
$password = 'root';
try {
$db = new PDO($dsn,$user,$password);
$stmt = $db->prepare('UPDATE post SET contents=:contents WHERE id=:id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':contents', $text, PDO::PARAM_STR);
// クエリの実行
$stmt->execute();
return "ok";
}
catch (PDOException $e) {
die ('エラー:' . $e->getMessage());
}
}
// 投稿削除用メソッド
public function delete ($id){
$dsn = 'mysql:host=localhost;dbname=board2;';
$user ='root';
$password = 'root';
try {
$db = new PDO($dsn,$user,$password);
$stmt = $db->prepare('delete from post where id=:id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
// クエリの実行
$stmt->execute();
return "ok";
}
catch (PDOException $e) {
die ('エラー:' . $e->getMessage());
}
}
}