Cara Koneksi Database Menggunaka PHP




Cara Mengkoneksi DataBase Menggunakan PHP






Kali ini kita akan membahas tentang bagaimana mengkoneksi Database sekaligus menampilkannya menggunakan PHP, Disini kita akan membuat 3 file PHP yang berisikan Codingan untuk koneksi dll. desain masih menggunakan Bootstrap agar lebih menarik dan enak dilihat.

  FIle yang kita perlukan :
  1. dao.php
  2. dbconfig.php
  3. index.php
  4. File Databese
 

Baiklah langsung saja kita coba.


1. Buat file index.php Baru



File ini berisi coding bagian desain tabel

Coding nya Begini Gan :


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Show Data with Mysql</title>
<!--coding ini untuk menggil atribut CSS desain Bootsrap-->
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="icon" href="assets/images/favicon.ico">
<link rel="stylesheet" href="assets/css/bootstrap.css">
<link rel="stylesheet" href="assets/css/floating-labels.css">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>

<?php
<!--Coding ini untuk memanggil file dao.php-->
include_once 'config/dao.php';
$dao = new Dao();
$result = $dao->read();
?>

<!--Desain tabel Dimulai dari sini-->
<div class="text-center">
<p style="font-family: consolas;font-size: 50px"><strong>TABEL USER</strong></p>
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">id</th>
<th scope="col">Username</th>
<th scope="col">Fullname</th>
<th scope="col">Password</th>
<th scope="col">Email</th>
<th scope="col">Telp</th>
<th scope="col">Baned</th>
<th scope="col">Login Time</th>
<th scope="col">Akses</th>
</tr>
</thead>
<tbody">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<th scope='row'>" . $row['id'] . "</th>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['fullname'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['telp'] . "</td>";
echo "<td>" . $row['banned'] . "</td>";
echo "<td>" . $row['logintime'] . "</td>";
echo "<td>" . $row['akses'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</body>
</html>




2. Buat file dao.php Baru



Disini kita akan membuat coding untuk memanggil tabel User yang akan kita tampilkan

Coding nya Begini Gan :


<?php
include_once 'dbconfig.php';
class Dao {
var $link;
public function __construct() {
$this->link = new Dbconfig();
}
public function read(){
$query ="SELECT * FROM users ORDER BY id ASC";
return mysqli_query( $this->link->conn,$query);
}
}




3. Buat file dbconfig.php Baru



Coding ini befungsi untuk koneksi database agar terkoneksi dan bisa ditampilkan di browser

Coding nya Begini Gan :


<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'proweb10a');

class Dbconfig{
var $conn;
function __construct() {
$this->conn =new mysqli(DB_HOST, DB_USER,DB_PASS, DB_NAME);
if($this->conn->connect_error){
echo $this->conn->connect_error;
exit();
}
}

}




4. Buat file Database Baru



Example

Coding nya Begini Gan :


-- phpMyAdmin SQL Dump
-- version 4.3.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Mar 28, 2018 at 05:23 AM
-- Server version: 5.6.24
-- PHP Version: 5.6.8

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `proweb10a`
--

-- --------------------------------------------------------

--
-- Table structure for table `alamat`
--

CREATE TABLE IF NOT EXISTS `alamat` (
`idalamat` varchar(11) NOT NULL,
`dusun` varchar(50) DEFAULT NULL,
`rtrw` varchar(11) DEFAULT NULL,
`jalan` varchar(50) DEFAULT NULL,
`kdpos` varchar(11) DEFAULT NULL,
`iddeskel` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `deskel`
--

CREATE TABLE IF NOT EXISTS `deskel` (
`iddeskel` varchar(11) NOT NULL,
`nama` varchar(30) DEFAULT NULL,
`idkec` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `kabkota`
--

CREATE TABLE IF NOT EXISTS `kabkota` (
`idkabkota` varchar(11) NOT NULL,
`nama` varchar(30) DEFAULT NULL,
`idprov` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `kecamatan`
--

CREATE TABLE IF NOT EXISTS `kecamatan` (
`idkec` varchar(11) NOT NULL,
`nama` varchar(30) DEFAULT NULL,
`idkabkota` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `provinsi`
--

CREATE TABLE IF NOT EXISTS `provinsi` (
`idprov` varchar(11) NOT NULL,
`nama` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`fullname` varchar(170) DEFAULT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`telp` varchar(30) DEFAULT NULL,
`banned` enum('N','Y') NOT NULL DEFAULT 'Y',
`logintime` datetime DEFAULT NULL,
`akses` int(1) NOT NULL,
`idalamat` varchar(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `fullname`, `password`, `email`, `telp`, `banned`, `logintime`, `akses`, `idalamat`) VALUES
(1, 'admin', 'administrator', '21232f297a57a5a743894a0e4a801fc3', 'admin@gmail.com', '087797503056', 'N', '2018-03-12 05:21:45', 1, NULL),
(2, 'waluyo', 'Alexander Waluyo', '*918EED48BA7A05C82BC22761D1A94AF6721CDF97', 'waluyo@gmail.com', '087797503050', 'N', '2018-03-29 05:21:45', 1, NULL),
(3, 'Zilong', 'Zilong Setiawan', '*5EB5E92DB79419D418EEAE11A06B91D768ACF8E6', 'jigong@gmail.com', '087797503059', 'N', '2018-03-26 05:21:11', 1, NULL);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `alamat`
--
ALTER TABLE `alamat`
ADD PRIMARY KEY (`idalamat`), ADD KEY `iddeskel` (`iddeskel`);

--
-- Indexes for table `deskel`
--
ALTER TABLE `deskel`
ADD PRIMARY KEY (`iddeskel`), ADD KEY `idkec` (`idkec`);

--
-- Indexes for table `kabkota`
--
ALTER TABLE `kabkota`
ADD PRIMARY KEY (`idkabkota`), ADD KEY `idprov` (`idprov`);

--
-- Indexes for table `kecamatan`
--
ALTER TABLE `kecamatan`
ADD PRIMARY KEY (`idkec`), ADD KEY `idkabkota` (`idkabkota`);

--
-- Indexes for table `provinsi`
--
ALTER TABLE `provinsi`
ADD PRIMARY KEY (`idprov`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `usernameunique` (`username`), ADD KEY `idalamat` (`idalamat`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;





Jika semua sudah sibat lakukan maka tampilannya akan seperti ini.



0 Response to "Cara Koneksi Database Menggunaka PHP"

Posting Komentar