90 likes | 247 Views
Pemrograman Web. Database Create-Retrieve-Update-Delete (CRUD). Definisi Tabel. Database Name: test Table Name: bukutamu CREATE TABLE IF NOT EXISTS `bukutamu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nama` varchar(50) NOT NULL, `email` varchar(50) NOT NULL,
E N D
Pemrograman Web Database Create-Retrieve-Update-Delete (CRUD)
DefinisiTabel • Database Name: test • Table Name: bukutamu CREATE TABLE IF NOT EXISTS `bukutamu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nama` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `pesan` mediumtext NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Eksekusi MySQL Query Generik via PHP $host = '127.0.0.1'; // localhost $db = 'test'; $user = 'root'; $pass = ""; // the password is an empty string if($con = mysql_connect($host, $user, $pass)) { mysql_select_db($db, $con); $sql = "..."; // put your query here... $result = mysql_query( $sql ); }
Create • Memasukkan data/informasimelaluisebuah form kedalamtabel di dalam database MySQL • DML INSERT $sql = "INSERT INTO bukutamu (nama, email, pesan) VALUES( '$nama', '$email', '$pesan' ); // $nama, $email, dan $pesan // diperolehdari $_POST atau $_GET
Retreive / Read • Mengambil data/informasidaridalamtabel di dalam database MySQL • DML SELECT $sql = "SELECT nama, email, pesan FROM bukutamu;" • Fetch array dari query result: while( $row = mysql_fetch_array ( $result ) ) { echo "<p>" . $row['nama'] . " – "; echo "$row['email'] . " – "; echo "$row['pesan'] . "<p>"; }
Update • Memperbarui data/informasipadatabel di dalam database MySQL • DML UPDATE $sql = "UPDATE bukutamu SET nama = '$nama', email = '$email', pesan = '$pesan' WHERE id = '$id';" // $nama, $email, $pesan, dan $id // diperolehdari $_POST atau $_GET // $id adalah PRIMARY KEY padatabelbukutamu
Delete • Menghapus data/informasi/barispadatabeldi dalam database MySQL • DML DELETE $sql = "DELETE FROM bukutamu WHERE id = '$id';" // $id diperolehdari $_POST atau $_GET // $id adalah PRIMARY KEY padatabelbukutamu
PHP-MySQL Administration Helper Tools • GunakanphpMyAdmin (PMA) • Penggunaan • Download dari:http://www.phpmyadmin.net/home_page/index.php • Extract zip/.tar.gz file yang di-download di root directory webserver (umumnyadalam folder htdocs/) • Copy/rename file config.sample.inc.phpphpMyAdminmenjadiconfig.inc.php • Buka file config.inc.php, ubahkonfigurasiyangdibutuhkan, dan save. • Aksesdari http://localhost/phpMyAdmin-[versi]-[lang]