Import mysql dump using php

后端 未结 4 944
北荒
北荒 2021-02-10 14:06

I have mysql dump file. How I can import this file into mysql using php?

4条回答
  •  庸人自扰
    2021-02-10 14:18

    I you want to use PHP, you can easily use the file() function, which reads a file into an array line by line.

    You could try something like that:

    mysql_connect(BD_HOST, DB_USER, BD_PASS) or die("Error connecting to MySQL server: ".mysql_error());
    mysql_select_db(DB_NAME);
    
    $all_lines = file("mysql_dump.sql", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
    
    foreach($all_lines as $query) {
        if(substr($query, 0, 2) == "--") {
            continue;
        }
    
        mysql_query($query) or die("Error executing query 
      "$query\"
    ".mysql_error()); }

提交回复
热议问题