如果我有一个函数,如果错误数组为空,则返回 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: ");
}
}