If you want to backup total database using php code then you can follow this code. In this code which backup total db , if you want to backup some table then you can also use this code. This is simple code.

<?php

// call function which backup database
backup_database('localhost', 'root', '', 'database_name', '*');

// this is main function which process all table data and ready for export
// first host name, db_username, db_password, db_name, when use * then all table
function backup_database($hostname, $user, $pass, $dbname, $tables = '*')
{
    $link = mysqli_connect($hostname, $user, $pass, $dbname);

    if (mysqli_connect_errno()) {
        echo "Failed to connect: " . mysqli_connect_error();
        exit;
    }

    // set utf8 character set
    mysqli_query($link, "SET NAMES 'utf8'");

    // get all table when * and insert all table into tales array
    if ($tables == '*') {
        $tables = array();
        $result = mysqli_query($link, 'SHOW TABLES');
        while ($row = mysqli_fetch_row($result)) {
            $tables[] = $row[0];
        }
    } else {
        // when table name specified then process
        $tables = is_array($tables) ? $tables : explode(',', $tables);
    }

    $return = '';
    // process table and process filed using this loop
    foreach ($tables as $table) {
        $result = mysqli_query($link, 'SELECT * FROM ' . $table);

        // get all fields and process them
        $num_fields = mysqli_num_fields($result);
        $num_rows = mysqli_num_rows($result);

        $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
        $row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE ' . $table));
        $return .= "\n\n" . $row2[1] . ";\n\n";
        $counter = 1;

        // process all fields under this table
        for ($i = 0; $i < $num_fields; $i++) {
            while ($row = mysqli_fetch_row($result)) {
                // make table and format table
                if ($counter == 1) {
                    $return .= 'INSERT INTO ' . $table . ' VALUES(';
                } else {
                    $return .= '(';
                }
                // make all fields and format also
                for ($j = 0; $j < $num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = str_replace("\n", "\\n", $row[$j]);
                    if (isset($row[$j])) {
                        $return .= '"' . $row[$j] . '"';
                    } else {
                        $return .= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return .= ',';
                    }
                }
                // when all row done then return using end line
                if ($num_rows == $counter) {
                    $return .= ");\n";
                } else {
                    $return .= "),\n";
                }
                ++$counter;
            }
        }
        // return using new line
        $return .= "\n\n\n";
    }
    // ready database for export and make sql file
    $fileName = 'backup-' . $dbname . '.sql';
    $handle = fopen($fileName, 'w+');
    fwrite($handle, $return);
    if (fclose($handle)) {
        echo "Backup Done: " . $fileName;
        exit;
    }
}

?>

if you face any problem then please inform me. I will try to help you

Comment