Last active
January 28, 2017 04:28
-
-
Save inuvalogic/d10807e36b76c4c9699cbe4bfaa1c530 to your computer and use it in GitHub Desktop.
latihan membuat website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE latihan CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE artikel ( | |
id INT(11) AUTO_INCREMENT PRIMARY KEY, | |
judul VARCHAR(255) NOT NULL, | |
isi TEXT NOT NULL | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$hostname = 'localhost'; | |
$dbname = 'latihan'; | |
$username = 'root'; | |
$password = ''; | |
$pdo = new PDO('mysql:host='.$hostname.';dbname='.$dbname, $username, $password, array( | |
PDO::ATTR_PERSISTENT => true | |
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$hostname = 'localhost'; | |
$dbname = 'latihanlagi'; | |
$username = 'root'; | |
$password = ''; | |
try { | |
$pdo = new PDO('mysql:host='.$hostname.';dbname='.$dbname, $username, $password, array( | |
PDO::ATTR_PERSISTENT => true | |
)); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
} catch(PDOException $e){ | |
echo "Ada error gan: ".$e->getMessage(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$judul = "judul artikel yang baru"; | |
$isi = "isi artikel baru yang akan diinputkan ke database"; | |
$sql_insert = "INSERT INTO `artikel` (`judul`,`isi`) VALUES (?,?)"; | |
$query = $pdo->prepare($sql_insert); | |
$query->bindParam(1, $judul); | |
$query->bindParam(2, $isi); | |
$query->execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$sql_select = "SELECT * FROM `artikel`"; | |
$query = $pdo->prepare($sql_select); | |
$query->execute(); | |
$row = $query->fetchAll(); | |
foreach ($row as $data) { | |
echo "<h3>Judul = ".$data['judul']."</h3>"; | |
echo "<p>Isi = ".$data['isi']."</p>"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$judul = "ganti dengan judul baru"; | |
$isi = "ganti isi artikel dengan yang baru"; | |
$id = 1; | |
$sql_update = "UPDATE `artikel` SET `judul` = ?, `isi` = ? WHERE `id` = ?"; | |
$query = $pdo->prepare($sql_update); | |
$query->bindParam(1, $judul); | |
$query->bindParam(2, $isi); | |
$query->bindParam(3, $id); | |
$query->execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$id = 2; | |
$sql_delete = "DELETE FROM `artikel` WHERE `id` = ?"; | |
$query = $pdo->prepare($sql_delete); | |
$query->execute(array($id)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment