Php/func mysqli commit

来自菜鸟教程
跳转至:导航、​搜索

PHP mysqli commit()函数

MySQL PHP MySQLi参考

示例-面向对象的样式

关闭自动提交,进行一些查询,然后提交查询:

<?php

$mysqli = new mysqli("localhost","my_user","my_password","my_db");


if ($mysqli -> connect_errno) {

  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  
  exit();

  }

// Turn autocommit off
$mysqli -> autocommit(FALSE);


// Insert some values 
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)

VALUES ('Peter','Griffin',35)");

  $mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age) 

VALUES ('Glenn','Quagmire',33)");


// Commit transaction
if (!$mysqli -> commit()) {
  echo "Commit 
  transaction failed";
  exit();
}

$mysqli -> close();

?>



在底部查看程序样式的示例。

定义和用法

commit()/ mysqli_commit()函数为指定的数据库连接提交当前事务。

Tip: 也看看 autocommit()

功能,该功能可以打开或关闭自动提交数据库修改,并且 rollback()

函数,它回滚当前事务。

句法

面向对象的样式:

$mysqli -> commit(flags, name)

程序风格:

mysqli_commit(connection, flags, name)

参数值

参数 描述
connection 需要。指定要使用的MySQL连接
flags

可选的。常量:

  • MYSQLI_TRANS_COR_AND_CHAIN-附加“ AND CHAIN”
  • MYSQLI_TRANS_COR_AND_NO_CHAIN-附加“ AND NO CHAIN”
  • MYSQLI_TRANS_COR_RELEASE-附加“ RELEASE”
  • MYSQLI_TRANS_COR_NO_RELEASE-追加“ NO RELEASE”
name 可选的。承诺/*

name 如果指定此参数,则执行* /

技术细节

返回值: 成功则为真。失败时为假
PHP版本: 5+
PHP更新日志: PHP 5.5:添加了

flags and name 参数

示例-程序风格

关闭自动提交,进行一些查询,然后提交查询:

<?php

$con=mysqli_connect("localhost","my_user","my_password","my_db");


if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  
  exit;

  }

// Turn autocommit off
mysqli_autocommit($con,FALSE);


// Insert some values 
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)

VALUES ('Peter','Griffin',35)");

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 

VALUES ('Glenn','Quagmire',33)");


// Commit transaction
if (!$mysqli_commit($con)) {
  echo 
  "Commit transaction failed";
  exit();
}

// Close connection

 mysqli_close($con);
?>



MySQL PHP MySQLi参考