<?php
//these variables are used for database connection
$host = "localhost";
$user = "db_user";
$pass = "db_pass";
$database = "db_name";
//connect to the database.
$db = mysql_connect($host, $user, $pass);
mysql_select_db ($database); ?>
2. INSERT Statement :
<?php
//load database connection
untuk meload bisa menggunakan teknik include, hal ini untuk mengurangi kesalahan program
berikut contoh :
include "sambung.php" "perintah ini memanggil sambung PHP untuk koneksi ke database
//...
//ini Untuk PHP 5 ke atas
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$emailAddress = $_POST['email'];
$zipCode = $_POST['zip'];
//Perintah untuk memasukan ke table
$query = "INSERT INTO `people` (`firstName`,`lastName`,`email`,`zip`, `dateAdded`)
VALUES ('$firstName','$lastName','$emailAddress','$zipCode', NOW());";
//run the query to insert the person.
$result = mysql_query($query) OR die(mysql_error());
//let them know the person has been added.
echo "Data berhasil di masukan " . $firstName . " " . $lastName . " ke dalam Database."; ?>
3.Perintah UPDATE
<?php
//load database connection
//...
//we will pretend we're getting the info from a form.
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$emailAddress = $_POST['email'];
$zipCode = $_POST['zip'];
$userId = $_POST['userId'];
//Make the query to update the table.
$query = "UPDATE `people` SET firstName='$firstName', lastName='$lastName',
email='$emailAddress', zip='$zipcode' WHERE userid='$userId';";
//run the query to update the person.
$result = mysql_query($query) OR die(mysql_error());
//let them know the person has been added.
echo "Data berhasil di update " . $firstName . " " . $lastName . " Dalam database."; ?>
Update adalah suatu perintah mengganti suatu data ya telah di masukan kedatabase.
4. Perintah Delete :
<?php
//load database connection
//...
//get the userid of the person we're deleting.
$userId = $_POST['userId'];
//write the query to delete the person.
$query = "DELETE FROM `people` WHERE userid='$userId'";
//run the query to delete the person.
$result = mysql_query($query) OR die(mysql_error());
echo 'Data berhasil Di hapus.'; ?>
5. Perintah Select
Perintah di bawah ini di pakai umtuk mengambil data dari database :
<?php
//load database connection
//...
//make the query to run.
//Sort the last name in an ascending order (A-Z)
$query = "SELECT * FROM `people` ORDER BY lastName ASC";
$result = mysql_query($query) OR die(mysql_error());
//now we turn the results into an array and loop through them.
while($row = mysql_fetch_array($result))
{
$firstName = $row['firstName'];
$lastName = $row['lastName'];
echo 'This is: ' . $firstName . ' ' . $lastName . "<br/>\n";
} ?>
Untuk printah ini silakan ganti pada baris sini aja :
$query = "SELECT * FROM `people` ORDER BY dateAdded DESC LIMIT 5";
7. SELECT Statement (Counting the results)
Perintah model ini biasanya di pakai untuk membuat halaman login :
<?php
//ketikan dan include script koneksi database di sini
//...
//Untuk inialisasi Form,
$userName = $_POST['username'];
$password = $_POST['password'];
//make the query to run.
$query = "SELECT * FROM `people` WHERE username='$userName' AND password='$password'";
$result = mysql_query($query) OR die(mysql_error());
$count = mysql_num_rows($result);
if($count > 0) {
$row = mysql_fetch_array($result);
$firstName = $row['firstName'];
echo 'Selamat Datang, '.$firstName.'!<br/>';
}
else
{
echo 'Informasi salah';
} ?>
Tidak ada komentar:
Posting Komentar