Search code examples
phpmysql

Can't import a sql file exported from phpMyAdmin


Ok, this is the situation. I have backed up my database by going into phpMyAdmin and then, without selecting any database, clicking export. Now when I try importing the exported sql file into a fresh MySQL installation, I'm getting errors like http://pastebin.com/vDHdG2T6 (used pastebin as it's pretty long)

I know I should have used mysqldump instead as it would skip the information_schema table. Do you have any suggestions how to solve this or import that file without having to edit the million lines file?

I've also tried importing the file like: mysql -u root -p -f -h localhost < Documents/localhost.sql This did import the the databases but many of them were corrupt, missing tables and such...I used the -f so it would keep going regardless of errors.

Thanks in advance!

EDIT:

Thank you guys for the suggestions, I'm sure you were on the right track. However, in the meantime I ended up messing up my backup file as well so I had to write a little php code to extract the lines I need. Here it is in case someone has the same problem:

$data = file_get_contents('import.sql');


if (! isset($_GET['db'])) {
    //list databases
    echo '<ul>';
    preg_match_all("/Database: `(.*?)`/", $data, $dbs);

    foreach ($dbs[1] as $database){
        echo "<li><a href=\"index.php?db=$database\">$database</a></li>";
    }
    echo '</ul>';
} else {
    $database = $_GET['db'];
    //only get the queries for that db
    $data = explode(PHP_EOL, $data);
    $buffer = array();
    $buffer_started = false;

    foreach ($data as $key => $row){

        if (preg_match("/Database: `.*?`/", $row) && $buffer_started) {
            array_pop($buffer);
            break;
        }

        if (preg_match("/Database: `$database`/", $row)) {
            $buffer_started = true;
            continue;
        }

        if ($buffer_started) {
            array_push($buffer, $row);
        }


        unset($data[$key]);
    }

    echo '<textarea cols="128" rows="20">'.implode("\n", $buffer).'</textarea>';
}

Save this as index.php and put your sql into import.sql in the same directory, it will list the databases found in file and just click on whichever you want and it will show the queries in a textarea.

Good luck.


Solution

  • As suggested by beanland, I will answer my own question, with a slightly modified code from the above edit:

    error_reporting(E_ALL);
    $data = file_get_contents('import.sql');
    
    
    if (! isset($_GET['db'])) {
        //list databases
        echo '<ul>';
        preg_match_all("/Database: `(.*?)`/", $data, $dbs);
    
        foreach ($dbs[1] as $database){
            echo "<li><a href=\"index.php?db=$database\">$database</a></li>";
        }
        echo '</ul>';
    } else {
        $database = $_GET['db'];
        //only get the queries for that db
        $data = explode(PHP_EOL, $data);
        $buffer = array();
        $buffer_started = false;
    
        foreach ($data as $key => $row){
    
            if (preg_match("/Database: `.*?`/", $row) && $buffer_started) {
                array_pop($buffer);
                break;
            }
    
            if (preg_match("/Database: `$database`/", $row)) {
                $buffer_started = true;
                continue;
            }
    
            if ($buffer_started) {
                array_push($buffer, $row);
            }
    
    
            unset($data[$key]);
        }
    
        $data = implode("\n", $buffer);
    
        //convert all InnoDB to MyISAM
    
        $data = str_replace('InnoDB', 'MyISAM', $data);
    
        echo '<a href="index.php">Back</a><h1>'.$database.'</h1><textarea cols="128" rows="20">'.$data.'</textarea>';
    }