-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-database.php
166 lines (117 loc) · 3.73 KB
/
class-database.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
class database
{
// properties
private $dbHost = "localhost";
private $dbUser = "root";
private $dbPass = "";
private $dbName = "oop";
//constructor adalah method atau fungsi yang otomatis dilakukan saat instansiasi dilakukan
function __construct()
{
$this->dbHost;
$this->dbUser;
$this->dbPass;
$this->dbName;
}//end of function
//mthod koneksi mysql
function koneksi()
{
mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
mysql_select_db($this->dbName);
}//end of function
function addBuku($judul, $pengarang, $penerbit, $thnTerbit)
{
$query = "INSERT INTO buku SET judul = '$judul',
pengarang = '$pengarang',
penerbit = '$penerbit',
tahunTerbit = '$thnTerbit'";
$hasil = mysql_query($query);
if($hasil)echo "Data Buku sudah disimpan ke DB";
else echo "Data Buku gagal disimpan ke DB";
}//end of function
function tampilBuku()
{
echo "<table border='1' align='center' cellpadding='0.5' cellspacing='0'>";
echo "<tr><th align='center' colspan='5'>CRUD dengan OOP</th></tr>";
echo "<tr>
<th>Judul</th>
<th>Pengarang</th>
<th>Penerbit</th>
<th>Tahun Terbit</th>
<th>Action</th>
</tr>";
$sql = "SELECT * FROM buku";
$hasil = mysql_query($sql);
while($data=mysql_fetch_array($hasil))
{
echo "<tr>";
echo "<td>$data[judul]</td>";
echo "<td>$data[pengarang]</td>";
echo "<td>$data[penerbit]</td>";
echo "<td>$data[tahunTerbit]</td>";
echo "<td><a href='edit.php?ID=$data[id]'>Edit</a> | <a href='delete.php?ID=$data[id]'>Delete </a></td>";
echo "</tr>";
}// end of while
echo "</table>";
}//end of function
function hapusBuku($id)
{
$sql = "DELETE FROM buku WHERE id = $id";
$hasil = mysql_query($sql);
echo "Data buku ID = ".$id."Sudah di hapus";
}//end of function
function bacaDataBuku($type, $id)
{
$sql = "SELECT * FROM buku WHERE id = '$id'";
$hasil = mysql_query($sql);
$data = mysql_fetch_array($hasil);
if($type == "pengarang") return $data[pengarang];
else if($type == "id") return $data[id];
else if($type == "judul") return $data[judul];
else if($type == "penerbit") return $data[penerbit];
else if($type == "tahunTerbit") return $data[tahunTerbit];
}//end of function
function updateDataBuku($id, $judul, $pengarang, $penerbit, $thnTerbit)
{
$sql = "UPDATE buku SET judul = '$judul',
pengarang = '$pengarang',
penerbit = '$penerbit',
tahunTerbit = '$thnTerbit'
WHERE id = '$id'";
$hasil = mysql_query($sql);
if($hasil)
{
echo "Data buku sudah di Update";
echo "<script>window.location='index.php';</script>";
}
}// end of function
}//end of class
class searching
{
public $cari;
function cari($x)
{
$sql = "SELECT * FROM buku WHERE judul = '$x'";
$hasil = mysql_query($sql);
$data = mysql_fetch_array($hasil);
echo "<table border='1' align='center' cellpadding='0.5' cellspacing='0'>";
echo "<tr><th align='center' colspan='5'>CRUD dengan OOP</th></tr>";
echo "<tr>
<th>Judul</th>
<th>Pengarang</th>
<th>Penerbit</th>
<th>Tahun Terbit</th>
<th>Action</th>
</tr>";
echo "<tr>";
echo "<td>$data[judul]</td>";
echo "<td>$data[pengarang]</td>";
echo "<td>$data[penerbit]</td>";
echo "<td>$data[tahunTerbit]</td>";
echo "<td><a href='edit.php?ID=$data[id]'>Edit</a> | <a href='delete.php?ID=$data[id]'>Delete </a></td>";
echo "</tr>";
echo "</table>";
}
}//end of class
?>