Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Cách nhập / khôi phục bảng MySql bằng PHP

Nếu bạn muốn nhập bảng mysql vào thời gian chạy của ứng dụng php thì ở đây tôi sẽ chỉ cho bạn cách bạn có thể dễ dàng khôi phục bảng mysql bằng PHP. Nói chung, bạn sử dụng để nhập cơ sở dữ liệu mysql từ PHPMyAdmin, Đây là một trong những phương pháp dễ nhất để nhập cơ sở dữ liệu mysql nhưng nếu bạn đang tìm kiếm giải pháp để nhập cơ sở dữ liệu trong quá trình cài đặt ứng dụng php như wordpress, joomla, drupal, v.v. thì dưới đây là phương pháp PHP đơn giản cho nhập cơ sở dữ liệu mysql mà không có PHPMyAdmin.



Nhập bảng MySql bằng PHP

Sử dụng tập lệnh php sau để nhập / khôi phục các bảng cơ sở dữ liệu mysql.

<?php
    // Set database credentials
   $hostname     = 'localhost'; // MySql Host
   $username = 'root'; // MySql Username
   $password = 'root'; // MySql Password
   $dbname     = 'dbname'; // MySql Database Name
   // File Path which need to import
   $filePath   = 'sql_files/mysql_db.sql';
 
    // Connect & select the database
   $con = new mysqli($hostname, $username, $password, $dbname);
 
    // Temporary variable, used to store current query
    $templine = '';
 
    // Read in entire file
    $lines = file($filePath);
 
    $error = '';
 
    // Loop through each line
    foreach ($lines as $line){
        // Skip it if it's a comment
        if(substr($line, 0, 2) == '--' || $line == ''){
            continue;
        }
 
        // Add this line to the current segment
        $templine .= $line;
 
        // If it has a semicolon at the end, it's the end of the query
        if (substr(trim($line), -1, 1) == ';'){
            // Perform the query
            if(!$con->query($templine)){
                $error .= 'Error performing query "<b>' . $templine . '</b>": ' . $db->error . '<br /><br />';
            }
 
            // Reset temp variable to empty
            $templine = '';
        }
    }
    $con->close();
    echo  !empty($error)?$error:"Import Success";
?>




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Ví dụ về MAKETIME () - MySQL

  2. TIMEDIFF () so với SUBTIME () trong MySQL:Sự khác biệt là gì?

  3. Sử dụng truy vấn MySQL để duyệt qua các hàng để tạo một cây đệ quy

  4. MySQL không sử dụng chỉ mục với mệnh đề WHERE IN?

  5. Sử dụng Cơ sở dữ liệu quan hệ MySQL trên Fedora 12