Belajar PHP MySQLi

Lanjutan dari sini. Catatan iseng belajar PHP dan MySQLi.

Database connection

Setiap kali mau create, read, update, atau delete data, mesti konek dulu ke database.

<?php
// host, user, password, database name
$link = mysqli_connect('localhost','thole','topsecret','sekolah');
// cek error
if(!$link){
   die('ada error' . mysqli_connect_error());
}
// menutup koneksi
mysqli_close($link);
?>

Create new data

Cara menambah data baru.

<?php
// host, user, password, database name
$link = mysqli_connect('localhost','thole','topsecret','sekolah');
// cek error
if(!$link){
   die('ada error' . mysqli_connect_error());
}
// input data
$query = "INSERT INTO murid (nama, umur, alamat) VALUES ('Foo','17','Somewhere')";
$hasil = mysqli_query($link, $query);
// cek apakah berhasil
if($hasil) {
   echo "Berhasil!";
}
// menutup koneksi
mysqli_close($link);
?>

Bisa juga menambah beberapa data (multi row) sekaligus, misal data murid A, B, C, dsb.

<?php
// host, user, password, database name
$link = mysqli_connect('localhost','thole','topsecret','sekolah');
// cek error
if(!$link){
   die('ada error' . mysqli_connect_error());
}
// input multi data
$query = "INSERT INTO murid (nama, umur, alamat) VALUES ('Joo','27','Kota XXX');";
$query .= "INSERT INTO murid (nama, umur, alamat) VALUES ('Sis','35','Kota ZZZ');";
$query .= "INSERT INTO murid (nama, umur, alamat) VALUES ('Doe','16','Kota YYY')";
$hasil = mysqli_multi_query($link, $query);
// cek apakah berhasil
if($hasil) {
   echo "Berhasil!";
}
// menutup koneksi
mysqli_close($link);
?>

Read data

Menampilkan semua data. $query = "SELECT * FROM murid";

<?php
// host, user, password, database
$link = mysqli_connect('localhost','thole','topsecret','sekolah');
// cek error
if(!$link){
   die('ada error' . mysqli_connect_error());
}
// tampilkan data
$query = "SELECT * FROM murid";
$hasil = mysqli_query($link, $query);
if(mysqli_num_rows($hasil) > 0) {
   while ($data = mysqli_fetch_assoc($hasil)) {
      echo $data['nama'] . ", " . $data['umur'] . " thn, " . $data['alamat'] . "<br>";
   }
}

// menutup koneksi
mysqli_close($link);
?>

Bisa juga menampilkan data dengan kriteria tertentu, misalnya nama, alamat, dsb. Misal: untuk mencari murid yang bernama Thole, perintahnya adalah $query = "SELECT * FROM murid WHERE nama = 'Thole' ";

<?php
// host, user, password, database
$link = mysqli_connect('localhost','thole','topsecret','sekolah');
// cek error
if(!$link){
   die('ada error' . mysqli_connect_error());
}
// tampilkan data
$query = "SELECT * FROM murid WHERE nama='Thole' ";
$hasil = mysqli_query($link, $query);
if(mysqli_num_rows($hasil) > 0) {
   while ($data = mysqli_fetch_assoc($hasil)) {
      echo $data['nama'] . ", " . $data['umur'] . " thn, " . $data['alamat'] . "<br>";
   }
}
// menutup koneksi
mysqli_close($link);
?>

Update data

Mengedit/update data.

<?php
// host, user, password, database
$link = mysqli_connect('localhost','thole','topsecret','sekolah');
// cek error
if(!$link){
   die('ada error' . mysqli_connect_error());
}
// update data
$query = "UPDATE murid SET nama = 'Thole', umur = '27', alamat = 'DDD' WHERE id = 6";
$hasil = mysqli_query($link, $query);
if($hasil) {
   echo "Sukses..";
}
// menutup koneksi
mysqli_close($link);
?>

Delete data

Menghapus data.

<?php
// host, user, password, database
$link = mysqli_connect('localhost','thole','topsecret','sekolah');
// cek error
if(!$link){
   die('ada error' . mysqli_connect_error());
}
// delete data
$query = "DELETE FROM murid WHERE id = 3";
$hasil = mysqli_query($link, $query);
// cek apakah berhasil
if($hasil) {
   echo "Sukses..";
}
// menutup koneksi
mysqli_close($link);
?>

Selamat mencoba… semoga bermanfaat.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s