Rabu, 27 Juli 2011
Perintah - perintah dasar di PHP (select,update,insert,delete)
Posted on 01.27 by Unknown
Perintah di bawah ini di pakai untuk 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";
<?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.';?>
<?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.";
?>
<?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.";
?>
<?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);?>
Langganan:
Posting Komentar (Atom)
No Response to "Perintah - perintah dasar di PHP (select,update,insert,delete)"
:)) :)] ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} ~x( :-t b-( :-L x( =))
Leave A Reply