Praktek CRUD1
1. Membuat koneksi :
file : koneksi.php<?php
$host = 'localhost';
$name = 'dbikaba';
$username = 'root';
$password = '';
$mysqli = mysqli_connect($host, $username, $password, $name);
if (mysqli_connect_errno()) {
echo "Koneksi gagal : ".mysqli_connect_error();
}
?>
ATAU :
=========================================================
<?php
$mysqli = mysqli_connect('localhost', 'root', '', 'crud_db');
if (mysqli_connect_errno()) {
echo "Koneksi gagal : ".mysqli_connect_error();
}
?>
2. Halaman Index
file : index.php<?php
// Create database connection using config file
include_once("koneksi2.php");
// Fetch all users data from database
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<a href="add.php">Add New User</a><br/><br/>
<table width='80%' border=1>
<tr>
<th>Nomor</th> <th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th>
</tr>
<?php
$nomor =1;
while($user_data = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$nomor ++."</td>";
echo "<td>".$user_data['name']."</td>";
echo "<td>".$user_data['mobile']."</td>";
echo "<td>".$user_data['email']."</td>";
echo "<td><a href='edit.php?id=$user_data[id]'>Edit</a> | <a href='delete.php?id=$user_data[id]'>Delete</a></td></tr>";
}
?>
</table>
</body>
</html>
Komentar
Posting Komentar