2012-03-15 13 views
1

私は著者と書籍として2つのテーブルを持っています。著者テーブルからブックテーブルにデータを挿入したいと思います。 挿入が成功すると、作成者テーブルのデータが削除されます。成功しなかった場合、データは作成者テーブルから削除されません。 私はロールバックを使用したいが、ここで著者と書籍は異なるサーバーの2つの異なるデータベースである。 ビルド関数はphpとmysqlに存在しています。ロールバックのphp-mysql代替機能

+0

2つのデータベースはどちらもInnoDBですか? – biziclop

+0

あなたはこれまでどんなコードを書いていますか?私たちがあなたを助けることができるようにコードを示してください。 –

+0

yes.twoデータベースは両方ともInnoDB – salma

答えて

0

まず挿入著者テーブルからすべての行データが

delete from database2.author where id in (select id from database1.book); 
0

トランザクションが単一の「データベース」内に隔離されている帳テーブルに挿入された場合、IDフィールドを使用して確認し、次に表

insert into database1.book (fields_BookTable) 
    select fields_aAuthor_inBookTable from database2.author 

を予約します。

実際に行うべきことは、両方のデータベースを使用するために単一のデータベース接続を使用することです。

「データベース」は、他のサーバーの「カタログ」または「スキーマ」によく似ています。アクセス許可と同じ接続内の別のデータベースのテーブルを使用できます。 PHP(未テスト)を介して取引を管理するための

$objConnect = mysql_connect("localhost","root","root") or die(mysql_error()); 
$objDB = mysql_select_db("mydatabase"); 

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

$query = "query1 comes here...";  

if(mysql_query($query)) { 
    // Commit Transaction 
    mysql_query("COMMIT"); 
    echo "Save Done."; 
} 
else { 
    // RollBack Transaction 
    mysql_query("ROLLBACK"); 
    echo "Error Save [".$strSQL."]"; 
} 
mysql_close($objConnect); 

そして、このチェック:mysql拡張と数値PK列を想定し、PHPによって制御http://dev.mysql.com/doc/refman/5.0/en/commit.html

+0

ここでは、データベースは2つの異なるサーバー – salma

0

溶液(NBますでしょうたとえば、2つの異なるサーバーがある場合など、トランザクション内で絶対にラップできない場合にのみ実行します。

// Server connections 
$srcConn = mysql_connect('somewhere', 'user', 'pass'); 
$dstConn = mysql_connect('elsewhere', 'user', 'pass'); 

// Fetch data to move into an array 
$query = " 
    SELECT * 
    FROM `srcdb`.`table` 
"; 
$result = mysql_query($query, $srcConn); 
$data = array(); 
while ($row = mysql_fetch_assoc($result)) $data[] = $row; 
mysql_free_result($result); 

// Rows to handle per cycle 
// A larger number reduces the number queries, but you need to respect the MySQL 
// max_allowed_packet and it will also increase PHP memory usage 
$rowsPerCycle = 100; 

// Name of the primary key column 
$pKName = 'id'; 

// This will hold the column names in the right order, without the PK 
// You could make the list static if you know what it will be 
// You may also need to keep the PK value 
$colNames = array(); 
foreach ($data[0] as $colName => $val) { 
    if ($colName != $pKName) { // Remove me if you need to keep the PK values 
    $colNames[] = $colName; 
    } 
} 

// Keep looping while $data still has some elements 
while ($data) { 

    // Remove $rowsPerCycle elements from the beginning of $data 
    $cycleData = array_splice($data, 0, $rowsPerCycle); 

    // This array holds a list of the primary keys we are migrating 
    $cycleIds = array(); 

    // Build the base query 
    $query = " 
    INSERT INTO `destdb`.`table` 
     (`".implode("`, `", $colNames)."`) 
    VALUES 
    "; 

    // Loop the rows and append them to the query (after escaping them, of course...) 
    foreach ($cycleData as $row) { 
    $cycleIds[] = $row[$pKName]; 
    unset($row[$pKName]); // Remove me if you need to keep the PK values 
    $query .= "\n('" . implode("', '", array_map('mysql_real_escape_string', $row, array_fill(0, count($row), $dstConn)) . "'),"; 
    } 

    // Do the insert 
    if (!mysql_query($query, $dstConn)) { 
    // Handle insert errors here 
    trigger_error('MySQL Error: '.mysql_error($dstConn).'; Query: '.$query); 
    } 

    // Do the delete 
    $query = " 
    DELETE FROM `srcdb`.`table` 
    WHERE `$pKName` IN (".implode(', ', $cycleIds).") 
    "; 
    if (!mysql_query($query, $srcConn)) { 
    // Handle insert errors here 
    trigger_error('MySQL Error: '.mysql_error($srcConn).'; Query: '.$query); 
    } 

}