简体   繁体   English

mySQL事务无法正常工作

[英]mySQL Transaction not working

I have the following code: 我有以下代码:

<?php

// Conexion a MySQL



$mysql_link = mysql_connect( 'localhost', 'root', '010101' );

if  (!$mysql_link) {
    die('No se pudo conectar a la DB: ' . mysql_error());

}

$mysql_db = mysql_select_db('test', $mysql_link);

if (!$mysql_db) {
die ('No se pudo seleccionar DB: ' . mysql_error());
    }

$mysql_doc_query = "INSERT INTO documents (name, wfid, docid, archivo) VALUES     ('{$CodDoc}: {$documentoNombre} de {$DNI}', '{$workflowNombre}', '{$documentoNombre}',     '{$archivoNombre}' );
      INSERT INTO keywords (document_id, keyword, value) VALUES (LAST_INSERT_ID(),     'DNI', '{$DNI}' ), (LAST_INSERT_ID(), 'Cuit Empleador',
      '{$cuitEmpleador}' ), (LAST_INSERT_ID(), 'DigitalizadoPor', '{$usuario}' ),
  (LAST_INSERT_ID(), 'Direccion IP', '{$IP}' ), (LAST_INSERT_ID(), 'Ubicacion',     CONCAT('pdfs/',LAST_INSERT_ID(),'.pdf') );";

   // Insert en mysql
 $log = fopen('/dev/shm/log.txt', 'w');
  if( $log ) {
      fwrite( $log, $mysql_doc_query );
  }

  mysql_query("START TRANSACTION");
  if (mysql_query($mysql_doc_query) == TRUE)
  {
      mysql_query("COMMIT");
  echo "\nCOMMIT!";
  }
    else {
      mysql_query("ROLLBACK");
      echo "\nROLLBACK!";
  }
  mysql_close($mysql_link);
fclose ($log);
?>

It's always giving me ROLLBACK but I don't understand why. 它总是给我ROLLBACK,但我不明白为什么。

Any clue on this? 这有什么线索吗? The code generated in the log.txt archive can be executed in PHP MY ADMIN without problems. log.txt存档中生成的代码可以在PHP MY ADMIN中执行而不会出现问题。 (I know the variables aren't referenced but this is part of a larger script). (我知道变量没有被引用,但这是一个更大的脚本的一部分)。

Thanks a lot. 非常感谢。

Your testing method is bad. 你的测试方法很糟糕。 mysql_query() returns a boolean FALSE on error, or a result hand on success. mysql_query()在出错时返回一个布尔值FALSE,或者在成功时返回结果。 By PHP's type conversion rules, a result handle tests equal to true. 通过PHP的类型转换规则,结果句柄测试等于true。 You must use the strict boolean comparison, and excplicitly test against false: 您必须使用严格的布尔比较,并对false进行明确的测试:

if (mysql_query($mysql_doc_query) !== FALSE)

You can't send two mysql queries at the same time. 您不能同时发送两个mysql查询。 You're sending two different INSERT queries. 您正在发送两个不同的INSERT查询。

Also you must send your queries without ';' 您还必须在没有';'的情况下发送您的查询 since it's added automatically. 因为它是自动添加的。

mysql_query() only supports one statement at a time. mysql_query()只支持一个语句 You are executing multiple INSERT statements in one go: 您正在一次执行多个INSERT语句:

$mysql_doc_query = "INSERT INTO documents (name, wfid, docid, archivo) VALUES     ('{$CodDoc}: {$documentoNombre} de {$DNI}', '{$workflowNombre}', '{$documentoNombre}',     '{$archivoNombre}' );
      // Oops, new statement here! mysql_query() can't do that.
      INSERT INTO keywords (document_id, keyword, value) VALUES (LAST_INSERT_ID(),     'DNI', '{$DNI}' ), (LAST_INSERT_ID(), 'Cuit Empleador',
      '{$cuitEmpleador}' ), (LAST_INSERT_ID(), 'DigitalizadoPor', '{$usuario}' ),
  (LAST_INSERT_ID(), 'Direccion IP', '{$IP}' ), (LAST_INSERT_ID(), 'Ubicacion',     CONCAT('pdfs/',LAST_INSERT_ID(),'.pdf') );";

This could be debugged with mysql_error() inside your ROLLBACK block: 这可以使用ROLLBACK块中的mysql_error()进行调试:

  if (mysql_query($mysql_doc_query) == TRUE)
  {
      mysql_query("COMMIT");
  echo "\nCOMMIT!";
  }
  else {
    echo "Error in query: " . mysql_error();
    mysql_query("ROLLBACK");
    echo "\nROLLBACK!";
  }

If you need to do two INSERT s, you will need two separate calls to mysql_query() , and check for errors after each one. 如果需要执行两个INSERT ,则需要对mysql_query()两次单独调用,并在每次调用后检查错误。 On failure of either, do your ROLLBACK . 如果失败,请执行ROLLBACK

The standard way to run transactions consists in the following steps: 运行事务的标准方法包括以下步骤:

 mysql_query("SET AUTOCOMMIT=0"); //by default this is 1
 mysql_query("START TRANSACTION"); // start the transaction

 //run all the queries, possibly in a loop
 $q1 = mysql_query("INSERT INTO  .... ");
 $q2 = mysql_query("INSERT INTO  .... ");

 //check the success of all the queries and based on that commit or rollback
 if ($q1 and $q2) {
     mysql_query("COMMIT");
 } else {        
     mysql_query("ROLLBACK");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM