简体   繁体   English

使用mysqli_begin_transaction()时的服务器版本错误

[英]Server version error when using mysqli_begin_transaction()

I am building an application using a local server (Xammp). 我正在使用本地服务器(Xammp)构建应用程序。 First I turn off default autocommitt before beginning a transaction like so: 首先,在进行如下事务之前,我关闭了默认的自动提交功能:

      if($condition == true){
      mysqli_autocommit($mysqli, FALSE);//disabling default autocommit
      // Starting transaction for read and write  
      mysqli_begin_transaction($mysqli, MYSQLI_TRANS_START_READ_WRITE);
      //....Update table
       mysqli_commit($mysqli);//commit records to database
       }else {
         mysqli_rollback($mysqli);}

If I click a button, I am getting the error message telling me that my server version does not support mysqli_begin_transaction() minimum required is 5.6.5 .Tf i check in the database the results are as expected except that the user is not redirected anywhere,the browser displays this error message 如果单击按钮, mysqli_begin_transaction()收到错误消息,通知我我的服务器版本不支持mysqli_begin_transaction()最低要求是5.6.5如果在数据库中签入的结果与预期的一样,只是用户未重定向到任何地方, ,浏览器显示此错误消息

Warning: mysqli_begin_transaction(): This server version doesn't support 'READ WRITE' and 'READ ONLY'. 警告:mysqli_begin_transaction():此服务器版本不支持“读写”和“仅读”。 Minimum 5.6.5 is required in C:\\xampp\\htdocs\\wezhira\\foo.php on line 38 第38行的C:\\ xampp \\ htdocs \\ wezhira \\ foo.php至少需要5.6.5

Is there anyone who can help me on how to upgrade my server to test my application any links blogs or articles will be helpful,secondly,Will this error disappear if I use a live server.I have seen some solutions here on Stackoverflow suggesting that mysqli_begin_transaction() can be replaced by mysqli_autocommit($mysqli, FALSE); 有没有人可以帮助我如何升级服务器以测试我的应用程序,任何链接博客或文章都将对您有帮助,其次,如果使用实时服务器,此错误是否会消失。我在Stackoverflow上看到了一些解决方案,建议使用mysqli_begin_transaction()可以替换为mysqli_autocommit($mysqli, FALSE); How true is this statement? 这句话的真实性如何?

I realised that this error is due to the innodB version your mysql is running currently. 我意识到此错误是由于mysql当前正在运行的innodB版本引起的。 Xampp comes with MariaDB and their latest version of innodB is 5.6.34-79.1. Xampp随附MariaDB,其最新版本的innodB为5.6.34-79.1。 So you might want to upgrade the db or use autocommit() instead. 因此,您可能想升级数据库或使用autocommit()代替。

Run this code on your phpmyadmin SQL query to find out your version of Innodb 在您的phpmyadmin SQL查询上运行此代码,以查找您的Innodb版本

SHOW GLOBAL VARIABLES LIKE '%version%'

if your version is truely below 5.6.5 then, consider re-writing your code with autocommit() which is quite similar to transaction. 如果您的版本确实低于5.6.5,请考虑使用与事务非常相似的autocommit()重写代码。 To use autocommit, refer to this php documentation php autocommit documentation 要使用自动提交,请参阅此php文档php自动提交文档

autocommit() will work on your current version of innodB. autocommit()将在您当前的innodB版本上工作。 little changes are required when migrating your code from Transaction to autocommit(). 将代码从Transaction迁移到autocommit()时,几乎不需要进行任何更改。 Here is a code sample. 这是一个代码示例。

function failed_to_pay($package, $payer){
    global $sponsor;
    global $payer_id;
    require 'config.php';

    $mysqli = new mysqli($db_host, $db_user, $db_password, $db_name); //New connection to the Database

        if ($mysqli->connect_errno) {
            printf("Connect failed: %s\n", $mysqli->connect_error); //Check for Error in connection
            exit();
        }


        try{
            $mysqli->autocommit(FALSE); //Begin Transaction Using Autocommit function set to false
            $flag = true; //Use this to check for error

            $update_sponsor = $mysqli->query("UPDATE $package SET $payer = '' WHERE id = '$sponsor'");
                if(!$update_sponsor){
                    $flag = FALSE;
                }

            $delete_payer = $mysqli->query("DELETE FROM $package WHERE id = '$payer_id'");
                if(!$delete_payer){
                    $flag = FALSE;
                }

            if ($flag){
                $mysqli->commit(); //commit the transaction
                $mysqli->autocommit(TRUE); //set auto commit to true
                $mysqli->close();
                echo '<div class="alert alert-success"> You have been exited from this Package Successfully</div>';

                echo '<script>window.setTimeout(function() {
                window.location.href = "manage_acct.php";
                }, 3000);</script>';
            }
            else{
                $mysqli->rollback();
                $mysqli->autocommit(TRUE);
                $mysqli->close();
                echo '<div class="alert alert-warning"> Oops! We could not exit you from the Package. Please try again Later</div>';

            }

        }

        catch(Exception $e){
            $mysqli->rollback();
            $mysqli->autocommit(TRUE);
            $mysqli->close();
            echo '<div class="alert alert-warning"> Oops! An Error occurred: '. $e . '</div>';
        }
}

If MySQL / InnoDB is up to date - try to check php version. 如果MySQL / InnoDB是最新的-尝试检查php版本。 I changed from 7.3 to 7.2 and it helps. 我从7.3更改为7.2,它有所帮助。

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

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