gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:13:54 26 4
gpt4 key购买 nike

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

我没有足够的经验来解决此错误,希望专家提供一些帮助。我已经在发生错误的地方发布了我的代码,但如果您需要查看其他内容,请告知。错误发生在第 109 行。谢谢

PHP V5.3.13MYSQL V5.5.24APACHE 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.

最佳答案

来自 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");

关于php - 调用未定义函数 mysqli_begin_transaction() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com