Home

Operasi Dasar MySQL Menggunakan PHP

Bahasan berikut merupakan potongan-potongan program yang berfungsi untuk melakukan operasi dasar basis data yaitu memasukkan data, menampilkan data, mengupdate data dan menghapus data. Jadi bahasan ini lebih kearah dokumentasi program dan bukan merupakan program utuh.

Program aplikasi yang digunakan disini adalah AppServ Open Project - 2.5.10 for Windows. Program aplikasi tersebut terdiri dari Apache Web Server versi 2, PHP versi 5, MySQL versi 5 dan menyertakan phpMyAdmin versi 2 sebagai database manager.

Sebelumnya kita perlu menyiapkan basis data “db_buah” yang berisi “tbl_buah” yang terdiri dari “id” sebagai Primary Key dengan tipe Integer, “nama_buah” dengan tipe varchar, dan “rasa” dengan tipe varchar.

Berikut adalah potongan program yg kita gunakan untuk koneksi ke database.

 

define(”DB_SERVER”, “localhost”);
define(”DB_USER”, “usernya”);
define(”DB_PASS”, “passwordnya_user”);
define(”DB_NAME”, “db_buah”);

class Conn
{
public $mysqli;
function __construct()
{
$this->mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
}
function __destruct()
{
$this->mysqli->close();
}
}
?>

 

Berikut adalah potongan program yg kita gunakan untuk memasukkan data ke dalam database.

 














Nama Buah :
Rasanya :

 

$conn = new Conn();
$nama_buah = $_POST['nama_buah']);
$rasa = $_POST['rasa']);
$sql = “SELECT nama_buah FROM tbl_buah WHERE nama_buah=’$nama_buah’”;
$result = $conn->mysqli->query($sql);
if(!count($row = $result->fetch_row()) > 0) {
$sql = “INSERT INTO tbl_user (nama_buah, rasa) VALUES (’$nama_buah’, ‘$rasa’)”;
$conn->mysqli->query($sql);
echo “Data tersimpan”;
}
else { echo “Maaf, data tersebut sudah terdaftar.”;}
?>

 

Berikut adalah cara untuk menampilkan data.

$conn = new Conn();
$sql = “SELECT * FROM tabel_buah ‘”;
$resultCheck = $conn->mysqli->query($sql);
$rowCheck = $resultCheck->fetch_row();
if($rowCheck > 0)
{
echo “

”;
$result = $conn->mysqli->query($sql);
while($row = $result->fetch_assoc())
{
echo “”;
}
echo “
Nama BuahRasa
$row[nama_buah]$row[rasa]
”;
$result->close();
}
else
{
echo “Data tidak ditemukan”;
}
?>

 

Untuk mengupdate data, potongan program berikut perlu dikombinasikan dengan potongan program menampilkan data di atas yaitu untuk mendapatkan nilai “id” yang ingin diupdate. Berikut adalah cara untuk mengupdate data.

$id=$_GET['id'];
$conn = new Conn();
$sql = “SELECT * FROM tbl_buah WHERE tbl_buah.id=$id “;
$result = $conn->mysqli->query($sql);
$row = $result->fetch_assoc();


?>












Nama Buah : Rasanya :
/> />


”>

 

if(isset($_POST['submit']))
{
$conn = new Conn();
$id = $_GET['id'];
$nama_buah = $_GET['nama_buah'];
$rasa = $_GET['rasa'];
$sql = “UPDATE tbl_buah SET nama_buah=$nama_buah AND rasa=$rasa WHERE tbl_buah.id=’$id’ “;
$conn->mysqli->query($sql);
echo “Data sudah terupdate”;
}
?>

 

Untuk menghapus data, potongan program berikut perlu dikombinasikan dengan potongan program menampilkan data di atas yaitu untuk mendapatkan nilai “id” yang ingin dihapus. Berikut adalah cara untuk menghapus data.

$id=$_GET['id'];
$conn = new Conn();
$sql = “SELECT * FROM tbl_buah WHERE tbl_buah.id=$id “;
$result = $conn->mysqli->query($sql);
$row = $result->fetch_assoc();


?>












Nama Buah : Rasanya :
   


”>

 

if(isset($_POST['submit']))
{
$conn = new Conn();
$id = $_GET['id'];
$sql = “DELETE FROM tbl_buah WHERE tbl_buah.id=’$id’ LIMIT 1 “;
$conn->mysqli->query($sql);
echo “Data sudah terhapus”;
}
?>

 

SELAMAT MECOBA............

Comments
Search RSS
replica
replicahandbags (123.125.156.xxx) 2010-07-27 23:11:32

A wide array of replica balenciaga private Jamaica villas, cottages and chanel handbags apartments which are based on the designer handbags concept of home away from fendi handbags home and are more affordable than hermes replica handbags hotels.If this is your first replica handbags visit to the island, here bottega veneta bags is a list of towns and cities, cartier replica handbags you might like to visit.?Kingston?The bally bags ‘heartbeat of Jamaica', Kingston is not only burberry handbags the capital of the island but also its arts and Maurice Lacroix for sale culture hub. Over swiss watches the years, Kingston has witnessed Rolex for sale various disasters such as earthquakes and fires replica Rado , but each time it has risen from replica Longines the ashes, stronger and glorious than ever Audemars Piguet for sale . Kingston is yesterday and today – a fascinating Tag Heuer for sale blend of the new and the old. The main Rado replica attractions in the city are located Maurice Lacroix repli...
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Pemutakhiran Terakhir (Minggu, 02 Mei 2010 01:25)

 



Polls
Bagaimana pendapatmu tentang situs libiru?
 
Who's Online
Ada 1 tamu online
Statistik
Anggota : 60
Isi : 234
Links Situs : 1
Content View Hits : 121945