downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysqli::select_db> <mysqli::reap_async_query
Last updated: Fri, 13 Nov 2009

view this page in

mysqli::rollback

mysqli_rollback

(PHP 5)

mysqli::rollback -- mysqli_rollback現在のトランザクションをロールバックする

説明

オブジェクト指向型 (メソッド):

bool mysqli::rollback ( void )

手続き型:

bool mysqli_rollback ( mysqli $link )

データベースの現在のトランザクションをロールバックします。

パラメータ

link

手続き型のみ: mysqli_connect() あるいは mysqli_init() が返すリンク ID。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 オブジェクト指向型

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""world");

/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* 自動コミットを無効にします */
$mysqli->autocommit(FALSE);

$mysqli->query("CREATE TABLE myCity LIKE City");
$mysqli->query("ALTER TABLE myCity Type=InnoDB");
$mysqli->query("INSERT INTO myCity SELECT * FROM City LIMIT 50");

/* insert をコミットします */
$mysqli->commit();

/* すべての行を削除します */
$mysqli->query("DELETE FROM myCity");

if (
$result $mysqli->query("SELECT COUNT(*) FROM myCity")) {
    
$row $result->fetch_row();
    
printf("%d rows in table myCity.\n"$row[0]);
    
/* 結果を開放します */
    
$result->close();
}

/* ロールバックします */
$mysqli->rollback();

if (
$result $mysqli->query("SELECT COUNT(*) FROM myCity")) {
    
$row $result->fetch_row();
    
printf("%d rows in table myCity (after rollback).\n"$row[0]);
    
/* 結果を開放します */
    
$result->close();
}

/* myCity テーブルを削除します */
$mysqli->query("DROP TABLE myCity");

$mysqli->close();
?>

例2 手続き型

<?php
$link 
mysqli_connect("localhost""my_user""my_password""world");

/* 接続状況をチェックします */
if (mysqli_connect_errno()) {
    
printf("Connect failed: %s\n"mysqli_connect_error());
    exit();
}

/* 自動コミットを無効にします */
mysqli_autocommit($linkFALSE);

mysqli_query($link"CREATE TABLE myCity LIKE City");
mysqli_query($link"ALTER TABLE myCity Type=InnoDB");
mysqli_query($link"INSERT INTO myCity SELECT * FROM City LIMIT 50");

/* insert をコミットします */
mysqli_commit($link);

/* すべての行を削除します */
mysqli_query($link"DELETE FROM myCity");

if (
$result mysqli_query($link"SELECT COUNT(*) FROM myCity")) {
    
$row mysqli_fetch_row($result);
    
printf("%d rows in table myCity.\n"$row[0]);
    
/* 結果を開放します */
    
mysqli_free_result($result);
}

/* ロールバックします */
mysqli_rollback($link);

if (
$result mysqli_query($link"SELECT COUNT(*) FROM myCity")) {
    
$row mysqli_fetch_row($result);
    
printf("%d rows in table myCity (after rollback).\n"$row[0]);
    
/* 結果を開放します */
    
mysqli_free_result($result);
}

/* myCity テーブルを削除します */
mysqli_query($link"DROP TABLE myCity");

mysqli_close($link);
?>

上の例の出力は以下となります。

0 rows in table myCity.
50 rows in table myCity (after rollback).

参考

  • mysqli_commit() - 現在のトランザクションをコミットする
  • mysqli_autocommit() - データベース更新の自動コミットをオンまたはオフにする



add a note add a note User Contributed Notes
mysqli::rollback
jd at dilltree dot com
22-Jul-2009 11:08
Something to consider when using transact is that you should not perform a normal query on the same table (such as a DELETE) immediately after a transaction.  If the transaction rolls-back, the DELETE will execute and even show affected rows, but the row can be magically re-inserted even if the rollback() command comes before the DELETE query.
Lorenzo - webmaster AT 4tour DOT it
10-Feb-2009 02:31
This is an example to explain the powerful of the rollback and commit functions.
Let's suppose you want to be sure that all queries have to be executed without errors before writing data on the database.
Here's the code:

<?php
$all_query_ok
=true; // our control variable

//we make 4 inserts, the last one generates an error
//if at least one query returns an error we change our control variable
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (200)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (300)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; //duplicated PRIMARY KEY VALUE

//now let's test our control variable
$all_query_ok ? $mysqli->commit() : $mysqli->rollback();

$mysqli->close();
?>

hope to be helpful!

mysqli::select_db> <mysqli::reap_async_query
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites