Pertemuan VI OOP PHP MYSQL 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

OOP – PHP - MYSQL

Pertemuan XI
Persiapan
• Buat database hrd
• Buat tabel dtuser

CREATE TABLE `dtuser` (


`iduser` varchar(11),
`nama` varchar(100),
`alamat` varchar(100),
`usia` varchar(11)
)
Buat koneksi
• Buat folder hrd di xampp/htdocs/
• file koneksi.php, dengan coding sbb :
<?php
$host = "localhost";
$database = "hrd";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $database);
if(!$conn->connect_errno) {
echo 'Koneksi berhasil';}
else {
echo 'Koneksi gagal '.$conn->connect_error;
};
?>
Note : Silahkan ditrial dengan beda nama database dan perhatikan
Tambah data
Buat file input.php, dengan coding sbb :
<?php
$host = "localhost";
$database = "hrd";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $database);
$sql = "INSERT INTO dtuser VALUES ('8', 'ARIE ','Kediri', '19')";
if($conn->query($sql) == true) {
echo 'Data Masuk';
}else
{
echo 'Entri gagal';
};
?>
Note : Silahkan ditrial dengan beda nama table dan perhatikan yang
Multi Query
Buat file multi.php, dengan coding sbb :
<?php
$host = "localhost";
$database = "hrd";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $database);
$sql = "INSERT INTO dtuser VALUES (‘045', 'roland','Kediri', ‘27');";
$sql .= "INSERT INTO dtuser VALUES (‘066', 'Yessy','Kediri', ‘38')";
if($conn->multi_query($sql) == true) {
echo 'Data Masuk';
}else
{
echo 'Entri gagal';
};
$conn ->Close();
?>
Note :
ciri multi query -> 1. query pertama ditambahkan ; sebelum “
Tampilkan Data - cara fetch_assoc()
Buat file tampilassoc.php, dengan coding sbb :
<?php
$host = "localhost";
$database = "hrd";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $database);
$sql = "SELECT * FROM dtuser";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result-> fetch_assoc()) {
echo $row ['nama']. ' | ' .$row['alamat']. '</br>';
}
}
?>
Tampilkan Data - cara fetch_object()
Buat file tampilobject.php, dengan coding sbb :
<?php
$host = "localhost";
$database = "hrd";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $database);
$sql = "SELECT * FROM dtuser ";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result-> fetch_object()) {
echo $row->nama. ' | '.$row->alamat. '</br>';
}
}
else
{ echo 'hasil kosong'; }
Menghapus Data
Buat file hapus.php, dengan coding sbb :
<?php
$host = "localhost";
$database = "hrd";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $database);
$sql = "DELETE FROM dtuser WHERE nama = ' ARIE'";
if($conn->query($sql) == true)
{
echo 'Hapus berhasil';
}else
{
echo 'hapus gagal';
};
?>
Edit Data
Buat file update.php, dengan coding sbb :
<?php
$host = "localhost";
$database = "hrd";
$username = "root";
$password = "";
$conn = new mysqli($host, $username, $password, $database);
$sql = “UPDATE dtuser SET nama = 'Endang‘, alamat=‘madiun’ WHERE
nama = 'Dwi' ";
if($conn->query($sql) == true) {
echo 'Data berhasil diedit';
}else
{
echo 'Edit gagal';
};
Continued…

You might also like