-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.php
66 lines (56 loc) · 2.44 KB
/
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
<?php
class Database {
// Properti
public $host = "localhost";
public $username = "root";
public $password = "";
public $database = "db_php_0008";
public $connect;
function __construct() {
$this->connect = mysqli_connect($this->host, $this->username, $this->password, $this->database);
// Pengujian koneksi
if (mysqli_connect_errno()) {
die("Koneksi gagal: " . mysqli_connect_error());
}
}
function tambahData($nama, $alamat, $nohp, $kelas, $nim, $jenis_kelamin, $foto) {
mysqli_query($this->connect, "INSERT INTO tb_users_0008 (nama, alamat, nohp, kelas, nim, jenis_kelamin, foto)
VALUES ('$nama', '$alamat', '$nohp', '$kelas', '$nim', '$jenis_kelamin', '$foto')");
}
// Menampilkan data
function tampilData() {
$data = mysqli_query($this->connect, "SELECT * FROM tb_users_0008");
$rows = mysqli_fetch_all($data, MYSQLI_ASSOC);
return $rows;
}
// Mengedit data (menampilkan data user yang ingin diedit)
function editData($id) {
$stmt = $this->connect->prepare("SELECT * FROM tb_users_0008 WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $rows;
}
// Memperbarui data
function updateData($id, $nama, $alamat, $nohp, $kelas, $nim, $jenis_kelamin, $foto = null) {
if ($foto) {
$stmt = $this->connect->prepare("UPDATE users SET nama = ?, alamat = ?, nohp = ?, kelas = ?, nim = ?, jenis_kelamin = ?, foto = ? WHERE id = ?");
$stmt->bind_param("sssssssi", $nama, $alamat, $nohp, $kelas, $nim, $jenis_kelamin, $foto, $id);
} else {
$stmt = $this->connect->prepare("UPDATE users SET nama = ?, alamat = ?, nohp = ?, kelas = ?, nim = ?, jenis_kelamin = ? WHERE id = ?");
$stmt->bind_param("ssssssi", $nama, $alamat, $nohp, $kelas, $nim, $jenis_kelamin, $id);
}
$stmt->execute();
$stmt->close();
}
// Menghapus data
function delete($id) {
$stmt = $this->connect->prepare("DELETE FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
}
}
?>