调用未定义的函数mysqli_begin_transaction()错误

时间:2017-06-24 05:55:56

标签: php mysql

我正在使用一个PHP脚本来处理备份&恢复我的数据库。它有几个问题,但我会一次发布1个错误。当我单击恢复备份按钮时,我收到标题中的错误。

我没有足够的经验来解决这个错误,并希望得到专家的一些帮助。我已将代码发布到发生错误的位置,但如果您需要查看其他内容,请告知我们。错误发生在第109行。谢谢

PHP V5.3.13 MYSQL V5.5.24 APACHE V2.2.22

<?php

function restoredata($uploadedfile, &$ustartindex, &$uti, &$ulen, &$ucount, &$name, &$limit, $restoremethod)
{
 global $host, $username, $passwd, $charset, $port, $upload_path, $backup_path;
 $sql = '';
 $zip = new ZipArchive();
 $filename = $upload_path . '/' . $uploadedfile;
 if (!is_file($filename)) {
  $filename = $backup_path . '/' . $uploadedfile;
 }

 if ($zip->open($filename) !== TRUE) {
  return "Cannot open file\n.";
 }

 try {
  $dbName = $zip->getFromName('database.txt');
  if ($restoremethod == '0') {
   $conn = mysqli_connect($host, $username, $passwd, $dbName, $port);
  }
  else {
   if ($uti == 0 && $ustartindex == 0) {
    $conn = mysqli_connect($host, $username, $passwd, 'mysql', $port);
   }
   else {
    $conn = mysqli_connect($host, $username, $passwd, $dbName, $port);
   }
  }

  if (!$conn) {
   return 'Could not connect ' . mysqli_error($conn);
  }

  if (!mysqli_set_charset($conn, $charset)) {
   mysqli_query($conn, 'SET NAMES ' . $charset);
  }

  $tables = $zip->getFromName('tables.txt');
  if ($tables === false) {
   $zip->close();
   return "Could'nt find tables";
  }

  $limit = intval($zip->getFromName('limit_info.txt'));
  $tables = explode(',', $tables);
  $ulen = count($tables);
  if ($uti < $ulen) {
   if ($uti == 0 && $ustartindex == 0) {
    if ($restoremethod == '1') {
     $sql = $zip->getFromName($dbName . '.sql');
     if ($sql !== false) {
      $sql = explode(';' . chr(10) , $sql);
      for ($i = 0; $i < count($sql) - 1; $i++) {
       $result = mysqli_query($conn, $sql[$i] . ';');
       if (!$result) {
        mysqli_close($conn);
        $zip->close();
        return 'Could not run query 1: ' . mysqli_error($conn);
       }
      }
     }

     mysqli_close($conn);
     $conn = mysqli_connect($host, $username, $passwd, $dbName, $port);
     if (!$conn) {
      $zip->close();
      return 'Could not connect ';
     }

     for ($i = 0; $i < $ulen; $i++) {
      $name = $tables[$i];
      $table = '`' . $name . '`';
      $sql = $zip->getFromName($table . '.sql');
      if ($sql !== false) {
       $sql = explode(';' . chr(10) , $sql);
       for ($j = 0; $j < count($sql) - 1; $j++) {
        $result = mysqli_query($conn, $sql[$j] . ';');
        if (!$result) {
         mysqli_close($conn);
         $zip->close();
         return 'Could not run query 2: ' . mysqli_error($conn);
        }
       }
      }
     }
    }
   }

   mysqli_autocommit($conn, FALSE);

   // mysqli_begin_transaction ($conn, MYSQLI_TRANS_START_READ_ONLY );//READ ONLY transaction 

   mysqli_begin_transaction($conn, MYSQLI_TRANS_START_READ_WRITE); <---ERROR HERE
   $name = $tables[$uti];
   $table = '`' . $name . '`';
   $ucount = intval($zip->getFromName($table . '.txt'));
   $sql = $tables = $zip->getFromName($table . '/offset' . $ustartindex . '.sql');
   $nerrors = 0;
   $serrors = '';
   if ($sql !== false) {
    $sql = explode(';' . chr(10) , $sql);
    for ($i = 0; $i < count($sql) - 1; $i++) {
     $sql[$i] = str_replace('NULL', "''", $sql[$i]); //avoid NULL errors
     $result = mysqli_query($conn, $sql[$i] . ';');
     if (!$result) {
      $serrors.= 'Could not run query in ' . $table . ' : ' . mysqli_error($conn) . '<br />';
      $nerrors+= 1;
      break;
     }
    }

    $ustartindex+= $limit;
   }
   else {
    $uti+= 1;
    $ustartindex = 0;
   }

   if (!mysqli_commit($conn)) {
    return "Transaction commit failed";
   }

   if ($nerrors > 0) {
    mysqli_close($conn);
    $zip->close();
    return $serrors;
   }
  }

  mysqli_close($conn);
  $zip->close();
 }

 catch(Exception $e) {
  return var_dump($e->getMessage());
 }

 return true;
}
?>
I will add that this is not my script but it does all all I need it to do including showing the progress of the backup, which visually is better than just a white page.

I would if possible like to sort these errors out and have a working script. Many thanks. 

1 个答案:

答案 0 :(得分:1)

http://php.net/manual/en/mysqli.begin-transaction.php起,mysqli_begin_transaction仅支持从PHP 5.5.0开始。随着你的运行5.3它不会起作用。

您可以删除该事务,它会起作用,尤其是在您最初尝试设置只读事务时。同时删除提交。

我会删除以下行,因为这会依赖于您期望的交易。

mysqli_autocommit($conn, FALSE);

如果你需要留在5.3那​​么......

mysqli_query($conn, "START TRANSACTION");