-1

如果我有一个函数,如果错误数组为空,则返回 true,如果数组不为空,则返回数组。然后在一个单独的函数中,我检查该函数是否返回 true。如何从第一个函数中获取错误数组以将其显示给用户?

public function backup($filename, $tables = '')
{
    $err = array();
    // get all of the tables
    if (empty($tables)) {
        $tables = array();
        $result = Nemesis::query('SHOW TABLES');
        while ($row = $result->fetch_row()) {
            $tables[] = $row[0];
        }
    } elseif (is_array($tables)) {
        $tables = explode(',', $tables);
    } else {
        $err[] = 'If you are specifying tables to be backed up, you need to provide the script with an array.';
    }
    $result = Nemesis::select("*", $table);
    $result_q = Nemesis::query("SHOW CREATE TABLE {$table}");
    if (!$result && !$result_q) {
        $err[] = 'Backup queries failed.';
    }
    // Cycle through each provided table
    foreach ($tables as $table) {
        $num_fields = $result->field_count;
        // First part of the output - remove the table
        $return .= 'DROP TABLE ' . $table . ';<|||||||>';
        // Second part of the output - create table
        $result_q_row = $result_q->fetch_row();
        $return .= "\n\n" . $result_q_row[1] . ";<|||||||>\n\n";
        // Third part of the output - insert values into new table
        for ($i = 0; $i < $num_fields; $i++) {
            while ($row = $result->fetch_row()) {
                $return .= 'INSERT INTO ' . $table . ' VALUES(';
                for ($j = 0; $j < $num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
                    if (isset($row[$j])) {
                        $return .= '"' . $row[$j] . '"';
                    } else {
                        $return .= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return .= ',';
                    }
                }
                $return .= ");<|||||||>\n";
            }
        }
        $return .= "\n\n\n";
    }
    $dirs = array($this->dir_backup, $this->dir_files, $this->dir_sql);
    foreach ($dirs as $dir) {
        if (!is_dir($dir)) {
            if (!mkdir($dir)) {
                $err[] = "Could not create {$dir}";
            }
        }
    }
    if (empty($err)) {
        $handle = fopen($this->dir_sql . $filename . '.sql', 'w+');
        fwrite($handle, $return);
        fclose($handle);
        //$arr = array(FRONTEND_IMAGE_UPLOAD_PATH, BACKEND_IMAGE_UPLOAD_PATH . 'users' . DIRECTORY_SEPARATOR);
        $zip = new ZipFolder($this->dir_files . $filename . '.zip', $this->folders_to_backup, $this->zip_ignore);
        return TRUE;
    } else {
        return implode('<br>', $err);
    }
}
public function backup_dbf () 
{
    $filename = 'dbbackup_' . date("d.m.Y_H_i_s");
    if ($this->backup($filename) === TRUE) {
        $msg = new Messages();
        $msg->add('s', "Backup {$filename} successfully created.");
    } else { 
        // somehow get err array
        $msg = new Messages();
        $msg->add('e', "Backup failed: ");
    }
}
4

3 回答 3

1

如果你试试这个怎么办:

public function backup_dbf () {
    $filename = 'dbbackup_' . date("d.m.Y_H_i_s");
    if (($error = $this->backup($filename)) === true) {
        $msg = new Messages();
        $msg->add('s', "Backup {$filename} successfully created.");
    } else { 
        $msg = new Messages();
        $msg->add('e', "Backup failed " . $error);
    }
}
于 2013-07-18T22:54:06.803 回答
0

您可以这样更新您的backup_dbf ()功能


public function backup_dbf () 
{
    $filename = 'dbbackup_' . date("d.m.Y_H_i_s");
    $output = $this->backup($filename);
    if (is_bool($output) && $output === TRUE) {
        $msg = new Messages();
        $msg->add('s', "Backup {$filename} successfully created.");
    } else { 
        // this means that you returned the err array from backup function
        echo print_r($output,true);
    }
}

于 2013-07-18T22:57:27.217 回答
0

您可以通过引用返回错误数组。

就像是

function yourFunction($array, &$errors = array()) {

    /* add errors to $errors */

    return true;
}

然后调用你的函数

$valid = yourFunction($myArray, $errors);

$errors将包含您的错误数组,并且$valid将是 true 或 false(布尔值)。

于 2013-07-18T22:46:59.207 回答