Beranda > IT > Bagaimana Cara Menggunakan PHP & MySQL dan Transaction (Begin,Commit, Rollback)

Bagaimana Cara Menggunakan PHP & MySQL dan Transaction (Begin,Commit, Rollback)

Tabel Tipe / Storage Engine : InnoDB

InnoDB Syntax :

CREATE TABLE `customer` (
.
.
.
.
) ENGINE=InnoDB;

#1289 – The ‘InnoDB’ feature is disabled; you need MySQL built with ‘InnoDB’ to have it working

Cara mengubah Storage Engine ke InnoDB di my.ini(C:\xampp\mysql\bin\my.ini)

Cari script berikut :

# Comment the following if you are using InnoDB tables
skip-innodb
#innodb_data_home_dir = “C:/xampp/mysql/”
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = “C:/xampp/mysql/”
#innodb_log_arch_dir = “C:/xampp/mysql/”
## You can set .._buffer_pool_size up to 50 – 80 %
## of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
## Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

Ubah menjadi :

# Comment the following if you are using InnoDB tables
#skip-innodb
innodb_data_home_dir = “C:/xampp/mysql/”
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = “C:/xampp/mysql/”
innodb_log_arch_dir = “C:/xampp/mysql/”
## You can set .._buffer_pool_size up to 50 – 80 %
## of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 16M
innodb_additional_mem_pool_size = 2M
## Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

Setelah itu stop xampp anda (C:\xampp -> Klik xampp_stop.exe)
Kemudian nyalakan lagi melalui XAMPP Control Panel

Cara mengubah tipe tabel menjadi InnoDB di PHPMyAdmin

1. Klik di tabel yang akan di cek (misal: tabel customer)
2. Klik Tab Operation
3. Lihat di bagian Storage Engine

Screenshoot

pastikan

Pastikan Storare Engine adalah InnoDB

Cara mengecek InnoDB di PHPMyAdmin sudah aktif atau belum

cek storage

Pastikan InnoDB sudah berwarna biru, apabila masih abu-abu berarti belum aktif.

Contoh Script Penggunaan PHP, MySQL dan Transaction

php_mysql_transaction1.php

<html>
<head>
<title>Commit Rollback Tutorial</title>
</head>
<body>
<form action="php_mysql_transaction2.php" name="frmAdd" method="post">
<table width="400" border="1">
<tr>
<td width="90"> <div align="left">CustomerID </div></td>
<td><input type="text" name="txtCustomerID" id="txtCustomerID" size="10"></td>
</tr>
<tr>
<td width="90"> <div align="left">Nama</div></td>
<td><input type="text" name="txtNama" id="txtNama" size="30"></td>
</tr>
<tr>
<td width="90"> <div align="left">Email </div></td>
<td><input type="text" name="txtEmail" id="txtEmail" size="20"></td>
</tr>
<tr>
<td width="90"> <div align="left">Kode Pos</div></td>
<td><input type="text" name="txtKodePos" id="txtKodePos" size="10"></td>
</tr>
<tr>
<td width="70"> <div align="left">Anggaran </div></td>
<td><input type="text" name="txtAnggaran" id="txtAnggaran" size="15"></td>
</tr>
<tr>
<td width="70"> <div align="left">Terpakai </div></td>
<td><input type="text" name="txtTerpakai" id="txtTerpakai" size="15"></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

php_mysql_transaction2.php

<html>
<head>
<title>Commit Rollback Tutorial</title>
</head>
<body>
<?php
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("ecommerce");

$customer_id = $_POST['txtCustomerID'];
$nama = $_POST['txtNama'];
$email = $_POST['txtEmail'];
$kode_pos = $_POST['txtKodePos'];
$anggaran = $_POST['txtAnggaran'];
$terpakai = $_POST['txtTerpakai'];

//*** Start Transaction ***//
mysql_query("BEGIN");

//***  Query 1 ***//
$strSQL = "INSERT INTO customer ";
$strSQL .="(customerid,nama,email,kodepos,anggaran,terpakai) ";
$strSQL .="VALUES (";
$strSQL .="$customer_id,";
$strSQL .="'$nama',";
$strSQL .="'$email',";
$strSQL .="$kode_pos,";
$strSQL .="$anggaran,";
$strSQL .="$terpakai";
$strSQL .=") ";
$objQuery1 = mysql_query($strSQL);
/*if(!$objQuery1)
{
mssql_query("ROLLBACK");
echo "Error Save [".$sql."]";
exit();
}*/

//***  Query 2 ***//
$strSQL = "INSERT INTO customer ";
$strSQL .="(customerid,nama,emaild,kodepos,anggaran,terpakai) ";
$strSQL .="VALUES (";
$strSQL .="$customer_id,";
$strSQL .="'$nama',";
$strSQL .="'$email',";
$strSQL .="$kode_pos,";
$strSQL .="$anggaran,";
$strSQL .="$terpakai";
$strSQL .=") ";
$objQuery2 = mysql_query($strSQL);
/*if(!$objQuery2)
{
mssql_query("ROLLBACK");
echo "Error Save [".$sql."]";
exit();
}*/

if(($objQuery1) and ($objQuery2))
{
//*** Commit Transaction ***//
mysql_query("COMMIT");
echo "Save Done.";
}
else
{
//*** RollBack Transaction ***//
mysql_query("ROLLBACK");
echo "Error Save [".$strSQL."]";
}
mysql_close($objConnect);
?>
</body>
</html>

Buatlah kedua file php di atas dan simpan di root-path/tes/ (direktori lokal saya, silakan sesuaikan dengan direktori anda masing-masing)

Kemudian jalankan pada browser  : http://localhost/tes/php_mysql_transaction1.php

Screenshoot

Screenshoot

About these ads
  1. Belum ada komentar.
  1. No trackbacks yet.

Berikan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Logout / Ubah )

Twitter picture

You are commenting using your Twitter account. Logout / Ubah )

Facebook photo

You are commenting using your Facebook account. Logout / Ubah )

Google+ photo

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Ikuti

Get every new post delivered to your Inbox.

%d blogger menyukai ini: