Ekspresi | Belajar HTML dan PHP | Sunda | Kontak | Gmail | Uang Adsense
Kuliah Sistem Informasi,
Jurnal Ilmiah Sistem Informasi
STMIK JABAR
LP3I Tasikmalaya
STAI Al-Falah
Belajar Pemrograman PHP, MySQL, Framework CI, Domain, Hosting dan CMS Wordpress
Inkubator | Daftar Isi | Bisnis Bitcoin | Tools Penting | PrivacyPolicy | Inggris Arab

Thursday, May 9, 2019

Belajar PHP dan MYSQLi Menampilkan Relasi Dua Tabel atau Lebih

Buatlah file 1krs.php
Copy-paste script ini:
<?php

//koneksi ke database mysql, silahkan di rubah dengan koneksi agan sendiri
$koneksi = mysqli_connect("localhost","root","","stmikj2");

//cek jika koneksi ke mysql gagal, maka akan tampil pesan berikut
if (mysqli_connect_errno()){
echo "Gagal melakukan koneksi ke MySQL: " . mysqli_connect_error();
}

//$sql = 'SELECT a.id, a.name,b.id FROM tutorials_inf a,tutorials_bks b WHERE a.id = b.id';
$sql = 'select * from 1mhs p inner join 1krs b on p.nim=b.nim';
$result = mysqli_query($koneksi,$sql);
?>

<table cellpadding="5" cellspacing="0" border="1">
<tr>
<th>NIM</th>
<th>Mahasiswa</th>
<th>KDMK</th>
</tr>
<?php while($buku = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $buku['nim'];?></td>
<td><?php echo $buku['nama'];?></td>
<td><?php echo $buku['kdmk'];?></td>
</tr>
<?php }?>
</table>

Jalankan di browser!

***
Buat file 2jurusan.php
Copy-paste script ini:
<?php

//koneksi ke database mysql, silahkan di rubah dengan koneksi agan sendiri
$koneksi = mysqli_connect("localhost","root","","stmikj2");

//cek jika koneksi ke mysql gagal, maka akan tampil pesan berikut
if (mysqli_connect_errno()){
echo "Gagal melakukan koneksi ke MySQL: " . mysqli_connect_error();
}

$sql = 'select * from 1mhs p inner join 1jurusan b on p.nim=b.nim';
$result = mysqli_query($koneksi,$sql);
?>

<table cellpadding="5" cellspacing="0" border="1">
<tr>
<th>NIM</th>
<th>Mahasiswa</th>
<th>Kode Jurusan</th>
</tr>
<?php while($buku = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $buku['nim'];?></td>
<td><?php echo $buku['nama'];?></td>
<td><?php echo $buku['kdj'];?></td>
</tr>
<?php }?>
</table>

Jalankan di browser!
***

Buat file 3krs.php
Copy-paste script ini:
<?php

//koneksi ke database mysql, silahkan di rubah dengan koneksi agan sendiri
$koneksi = mysqli_connect("localhost","root","","stmikj2");

//cek jika koneksi ke mysql gagal, maka akan tampil pesan berikut
if (mysqli_connect_errno()){
echo "Gagal melakukan koneksi ke MySQL: " . mysqli_connect_error();
}

$sql = 'select * from 1mhs p inner join 1krs b on p.nim=b.nim
inner join 1mk a on a.kdmk=b.kdmk';
$result = mysqli_query($koneksi,$sql);
?>

<table cellpadding="5" cellspacing="0" border="1">
<tr>
<th>NIM</th>
<th>Mahasiswa</th>
<th>KDMK</th>
<th>Mata Kuliah</th>
</tr>
<?php while($buku = mysqli_fetch_array($result)){?>
<tr>
<td><?php echo $buku['nim'];?></td>
<td><?php echo $buku['nama'];?></td>
<td><?php echo $buku['kdmk'];?></td>
<td><?php echo $buku['mk'];?></td>
</tr>
<?php }?>
</table>

Jalankan di browser!

***

Buat file 4krs
Copy-paste script ini:
<?php
//koneksi ke database mysql, silahkan di rubah dengan koneksi agan sendiri
$koneksi = mysqli_connect("localhost","root","","stmikj2");

//cek jika koneksi ke mysql gagal, maka akan tampil pesan berikut
if (mysqli_connect_errno()){
echo "Gagal melakukan koneksi ke MySQL: " . mysqli_connect_error();
}

$sql = 'select * from 1mhs p inner join 1krs b on p.nim=b.nim
inner join 1mk a on a.kdmk=b.kdmk where b.nim=1
';
$result = mysqli_query($koneksi,$sql);
?>
<table cellpadding="5" cellspacing="0" border="1">
<tr>
<th>NIM</th>
<th>Mahasiswa</th>
<th>KDMK</th>
<th>Mata Kuliah</th>
</tr>
<?php while($buku = mysqli_fetch_array($result))
{?>
<tr>
<td><?php echo $buku['nim'];?></td>
<td><?php echo $buku['nama'];?></td>
<td><?php echo $buku['kdmk'];?></td>
<td><?php echo $buku['mk'];?></td>
</tr>
<?php }?>
</table>

Jalankan di browser!

**

Buat file 4krscari
Copy-paste script ini:
<html>
<body>
<?php
//koneksi ke database mysql, silahkan di rubah dengan koneksi agan sendiri
$koneksi = mysqli_connect("localhost","root","","stmikj2");

//cek jika koneksi ke mysql gagal, maka akan tampil pesan berikut
if (mysqli_connect_errno()){
echo "Gagal melakukan koneksi ke MySQL: " . mysqli_connect_error();
}
?>

<form action="" method="get">
<label>Cari :</label>
<input type="text" name="cari">
<input type="submit" value="Cari">
</form>

<?php
if(isset($_GET['cari'])){
$cari = $_GET['cari'];
echo "<b>NIM: ".$cari."</b>";
}
?>

<table border="1">
<tr>
<th>KDMK</th>
<th>MK</th>
</tr>

<?php
if(isset($_GET['cari'])){
$cari = $_GET['cari'];

$data = mysqli_query($koneksi,"select * from 1mhs p inner join 1krs b on p.nim=b.nim
inner join 1mk a on a.kdmk=b.kdmk where b.nim like '%$cari'");
}
else{
$data = mysqli_query('select * from 1krs');
}
//$no = 1;
while($d = mysqli_fetch_array($data)){
?>
<tr>
<td><?php echo $d['kdmk'];?></td>
<td><?php echo $d['mk'];?></td>
</tr>
<?php } ?>
</table>

</body>
</html>

Jalankan di browser!
"Bitcoin and Forex are high risk business. We must join them smartly."
Belajar Bahasa Inggris dan Arab di WA dan FB Gratis!

No comments:

Post a Comment