PHP + MySQL Æ®·£Àè¼Ç ¿¹Á¦ | ¹øÈ£ : 538 ¹ø±Û Á¶È¸ : 2267 ȸ |
³ª´Â MySQL Æ®·£Àè¼ÇÀÌ »ç¿ëµÇ´Â PHP ÆÄÀÏÀÇ ÀϹÝÀûÀÎ ¿¹¸¦ ½ÇÁ¦·Î ¹ß°ßÇÏÁö ¸øÇß´Ù. ±× °£´ÜÇÑ ¿¹¸¦ º¸¿©ÁÙ ¼ö ÀÖ½À´Ï±î?
±×¸®°í ¶Ç ÇϳªÀÇ Áú¹®ÀÔ´Ï´Ù. ³ª´Â ÀÌ¹Ì ¸¹Àº ÇÁ·Î±×·¡¹ÖÀ»Çß°í Æ®·£Àè¼ÇÀ» »ç¿ëÇÏÁö ¾Ê¾Ò´Ù. header.php¿¡ PHP ÇÔ¼ö ¶Ç´Â ¾î¶² °ÍÀ» ³ÖÀ» ¼ö ÀÖ½À´Ï±î? ÇϳªÀÇ mysql_query°¡ ½ÇÆÐÇÏ¸é ´Ù¸¥ ½ÇÆеµ ¹ß»ýÇÕ´Ï´Ù.
³ª´Â ±×°ÍÀ» ¾Ë¾Æ ³Â´Ù°í »ý°¢ÇÑ´Ù. ±×·¸Áö?
mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");
$a1 = mysql_query("INSERT INTO rarara (l_id) VALUES('1')");
$a2 = mysql_query("INSERT INTO rarara (l_id) VALUES('2')");
if ($a1 and $a2) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
}
Æ®·£Àè¼ÇÀ¸·Î ÀÛ¾÷ ÇÒ ¶§ ÀϹÝÀûÀ¸·Î »ç¿ëÇÏ´Â ¾ÆÀ̵ð¾î´Â ´ÙÀ½°ú °°ÀÌ º¸ÀÔ´Ï´Ù (ÁØ ÀÇ»ç ÄÚµå).
try {
// First of all, let's begin a transaction
$db->beginTransaction();
// A set of queries; if one fails, an exception should be thrown
$db->query('first query');
$db->query('second query');
$db->query('third query');
// If we arrive here, it means that no exception was thrown
// i.e. no query has failed, and we can commit the transaction
$db->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
}
ÀÌ ¾ÆÀ̵ð¾î·Î Äõ¸®°¡ ½ÇÆÐÇϸé ExceptionÀÌ throwµÇ¾î¾ßÇÕ´Ï´Ù.
ºÒÇàÈ÷µµ ¸¶¹ý°ú °ü·ÃµÈ °ÍÀº ¾ø½À´Ï´Ù. ¾îµò°¡¿¡ Áö½Ã¸¦ ³»¸®°í Æ®·£Àè¼ÇÀ» ÀÚµ¿À¸·Î ó¸® ÇÒ ¼ö´Â ¾ø½À´Ï´Ù. Æ®·£Àè¼Ç¿¡¼ ½ÇÇàÇؾßÇÏ´Â Äõ¸® ±×·ìÀ» ÁöÁ¤ÇؾßÇÕ´Ï´Ù.
¿¹¸¦ µé¾î, Æ®·£Àè¼Ç (½ÃÀÛ Àü)°ú Æ®·£Àè¼Ç ÈÄ (Ä¿¹Ô ¶Ç´Â ·Ñ¹é ÈÄ)¿¡ ´Ù¸¥ µÎ °³ÀÇ Äõ¸®¸¦ »ç¿ëÇÏ¸é ¾î¶² ÀÏÀÌ ¹ß»ýÇß´ÂÁö¿¡ °ü°è¾øÀÌ Äõ¸®¸¦ ½ÇÇàÇϰԵ˴ϴÙ. ±×·¸Áö ¾ÊÀº °æ¿ì).
³ª´Â ±×°ÍÀ» ¾Ë¾Æ ³Â´Ù°í »ý°¢ÇÑ´Ù. ±×·¸Áö?
mysql_query("START TRANSACTION");
$a1 = mysql_query("INSERT INTO rarara (l_id) VALUES('1')");
$a2 = mysql_query("INSERT INTO rarara (l_id) VALUES('2')");
if ($a1 and $a2) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
}
<?php
// trans.php
function begin(){
mysql_query("BEGIN");
}
function commit(){
mysql_query("COMMIT");
}
function rollback(){
mysql_query("ROLLBACK");
}
mysql_connect("localhost","Dude1", "SuperSecret") or die(mysql_error());
mysql_select_db("bedrock") or die(mysql_error());
$query = "INSERT INTO employee (ssn,name,phone) values ('123-45-6789','Matt','1-800-555-1212')";
begin(); // transaction begins
$result = mysql_query($query);
if(!$result){
rollback(); // transaction rolls back
echo "transaction rolled back";
exit;
}else{
commit(); // transaction is committed
echo "Database transaction was successful";
}
?>
ÀÌ°ÍÀÌ "php mysql transaction"¿¡ ´ëÇÑ GoogleÀÇ Ã¹ ¹ø° °á°úÀ̱⠶§¹®¿¡, ¿ø·¡ ÀÛ¼ºÀÚ°¡ ¿øÇß´ø °Íó·³ mysqli·ÎÀ̸¦ ¼öÇàÇÏ´Â ¹æ¹ýÀ» ¸í½Ã ÀûÀ¸·Î º¸¿©ÁÖ´Â ´äº¯À» Ãß°¡ÇÑ´Ù°í »ý°¢Çß½À´Ï´Ù. ´ÙÀ½Àº PHP / mysqli¸¦ »ç¿ëÇÑ Æ®·£Àè¼ÇÀÇ °£´ÜÇÑ ¿¹ÀÔ´Ï´Ù.
// let's pretend that a user wants to create a new "group". we will do so
// while at the same time creating a "membership" for the group which
// consists solely of the user themselves (at first). accordingly, the group
// and membership records should be created together, or not at all.
// this sounds like a job for: TRANSACTIONS! (*cue music*)
$group_name = "The Thursday Thumpers";
$member_name = "EleventyOne";
$conn = new mysqli($db_host,$db_user,$db_passwd,$db_name); // error-check this
// note: this is meant for InnoDB tables. won't work with MyISAM tables.
try {
$conn->autocommit(FALSE); // i.e., start transaction
// assume that the TABLE groups has an auto_increment id field
$query = "INSERT INTO groups (name) ";
$query .= "VALUES ('$group_name')";
$result = $conn->query($query);
if ( !$result ) {
$result->free();
throw new Exception($conn->error);
}
$group_id = $conn->insert_id; // last auto_inc id from *this* connection
$query = "INSERT INTO group_membership (group_id,name) ";
$query .= "VALUES ('$group_id','$member_name')";
$result = $conn->query($query);
if ( !$result ) {
$result->free();
throw new Exception($conn->error);
}
// our SQL queries have been successful. commit them
// and go back to non-transaction mode.
$conn->commit();
$conn->autocommit(TRUE); // i.e., end transaction
}
catch ( Exception $e ) {
// before rolling back the transaction, you'd want
// to make sure that the exception was db-related
$conn->rollback();
$conn->autocommit(TRUE); // i.e., end transaction
}
¶ÇÇÑ PHP 5.5¿¡´Â »õ·Î¿î ¸Þ¼Òµå mysqli :: begin_transactionÀÌ ÀÖ½À´Ï´Ù. ±×·¯³ª ÀÌ°ÍÀº PHP ÆÀ¿¡ ÀÇÇØ ¾ÆÁ÷ ¹®¼ÈµÇÁö ¾Ê¾ÒÀ¸¸ç, PHP 5.3¿¡ ¾ÆÁ÷µµ ºÙ¾î ÀÖ½À´Ï´Ù. ±×·¡¼ ±×°Í¿¡ ´ëÇØ ¾ð±Þ ÇÒ ¼ö ¾ø½À´Ï´Ù.
»ç¿ëÁßÀÎ ½ºÅ丮Áö ¿£ÁøÀ» È®ÀÎÇϽʽÿÀ. ÀÌ°ÍÀÌ MyISAM ÀÎ °æ¿ì MyISAMÀÌ ¾Æ´Ñ InnoDB ½ºÅ丮Áö ¿£Áø ¸¸ Æ®·£Àè¼ÇÀ» Áö¿øÇϱ⠶§¹®¿¡ Transaction ( 'COMMIT', 'ROLLBACK')Àº Áö¿øµÇÁö ¾Ê½À´Ï´Ù.
Äõ¸® º¤Å͸¦ ¾ò°í Æ®·£Àè¼ÇÀ» ¼öÇàÇÏ´Â ÇÔ¼ö¸¦ ¸¸µé¾ú½À´Ï´Ù. ¾Æ¸¶ ´©±º°¡°¡ À¯¿ëÇÏ´Ù´Â °ÍÀ» ¾Ë°Ô µÉ °ÍÀÔ´Ï´Ù.
function transaction ($con, $Q){
mysqli_query($con, "START TRANSACTION");
for ($i = 0; $i < count ($Q); $i++){
if (!mysqli_query ($con, $Q[$i])){
echo 'Error! Info: <' . mysqli_error ($con) . '> Query: <' . $Q[$i] . '>';
break;
}
}
if ($i == count ($Q)){
mysqli_query($con, "COMMIT");
return 1;
}
else {
mysqli_query($con, "ROLLBACK");
return 0;
}
}
³ª´Â ÀÌ°ÍÀÌ ÀÖ¾úÁö¸¸ ÀÌ°ÍÀÌ ¿Ã¹Ù¸¥Áö È®½Å ÇÒ ¼ö ¾ø¾ú´Ù. ÀÌ°ÍÀ» ½ÃµµÇØ º¼ ¼öµµ ÀÖ½À´Ï´Ù.
mysql_query("START TRANSACTION");
$flag = true;
$query = "INSERT INTO testing (myid) VALUES ('test')";
$query2 = "INSERT INTO testing2 (myid2) VALUES ('test2')";
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
if (!$result) {
$flag = false;
}
$result = mysql_query($query2) or trigger_error(mysql_error(), E_USER_ERROR);
if (!$result) {
$flag = false;
}
if ($flag) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
}
PDO ¿¬°áÀ» »ç¿ëÇÒ ¶§ :
$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8', $user, $pass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // this is important
]);
Á¾Á¾ Æ®·£Àè¼Ç °ü¸®¸¦ À§ÇØ ´ÙÀ½ Äڵ带 »ç¿ëÇÕ´Ï´Ù.
function transaction(Closure $callback)
{
global $pdo; // let's assume our PDO connection is in a global var
// start the transaction outside of the try block, because
// you don't want to rollback a transaction that failed to start
$pdo->beginTransaction();
try
{
$callback();
$pdo->commit();
}
catch (Exception $e) // it's better to replace this with Throwable on PHP 7+
{
$pdo->rollBack();
throw $e; // we still have to complain about the exception
}
}
»ç¿ë ¿¹ :
transaction(function()
{
global $pdo;
$pdo->query('first query');
$pdo->query('second query');
$pdo->query('third query');
});
ÀÌ·¸°ÔÇϸé Æ®·£Àè¼Ç °ü¸® Äڵ尡 ÇÁ·ÎÁ§Æ® Àüü¿¡ Áߺ¹µÇÁö ¾Ê½À´Ï´Ù. ÀÌ°ÍÀº ÁÁÀº ÀÏÀÔ´Ï´Ù. ¿Ö³ÄÇϸéÀÌ ½º·¹µåÀÇ ´Ù¸¥ PDO °ü·Ã ´äº¯¿¡¼ º¼ ¶§, ½Ç¼ö¸¦ ÀúÁö¸£±â ½±½À´Ï´Ù. °¡Àå ÀϹÝÀûÀÎ °ÍµéÀº ¿¹¿Ü¸¦ µÇµ¹·Á ³õ°í try ºí·Ï ³»¿¡¼ Æ®·£Àè¼ÇÀ» ½ÃÀÛÇÏ´Â °ÍÀ» ÀØ¾î ¹ö¸®´Â °ÍÀÔ´Ï´Ù.
mysqli_multi_query¸¦ »ç¿ëÇÑ ÀýÂ÷ Àû ½ºÅ¸ÀÏ ¿¹Á¦´Â $ query°¡ ¼¼¹ÌÄÝ·ÐÀ¸·Î ºÐ¸® µÈ ¹®ÀåÀ¸·Î ä¿öÁ® ÀÖ´Ù°í °¡Á¤ÇÑ´Ù.
mysqli_begin_transaction ($link);
for (mysqli_multi_query ($link, $query);
mysqli_more_results ($link);
mysqli_next_result ($link) );
! mysqli_errno ($link) ?
mysqli_commit ($link) : mysqli_rollback ($link);
from https://stackoverflow.com/questions/2708237/php-mysql-transactions-examples by cc-by-sa and MIT lisence
ÀÌ Àü ±Û | MySQL CharacterSet º¯°æ |
´Ù À½ ±Û | [MySQL] 5.1ÀÇ InnoDB¿¡¼ MySQL Å×À̺í ÃÖÀûÈÇϱâ |
[°ü·Ã±Û] | ¾øÀ½ | ÀÛ¼ºÀϽà : 2020-04-23 13:36:14 |
Copyright(c) 2003 xirom all right reserved vol.3 Contact xirom@xirom.net form more information |