Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Bagaimana cara mengimpor file .sql di database mysql menggunakan PHP?

Peringatan: mysql_* ekstensi tidak digunakan lagi pada PHP 5.5.0, dan telah dihapus pada PHP 7.0.0. Sebagai gantinya, mysqli atau PDO_MySQL ekstensi harus digunakan. Lihat juga Ikhtisar MySQL API untuk bantuan lebih lanjut saat memilih API MySQL.
Jika memungkinkan, mengimpor file ke MySQL harus didelegasikan ke klien MySQL.

Saya punya cara lain untuk melakukan ini, coba ini

<?php

// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dump';

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// 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
    mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
    // Reset temp variable to empty
    $templine = '';
}
}
 echo "Tables imported successfully";
?>

Ini bekerja untuk saya



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara memasukkan bingkai data panda melalui mysqldb ke dalam database?

  2. Pilih TOP X (atau terbawah) persen untuk nilai numerik di MySQL

  3. MySQL mengonversi output timediff ke format hari, jam, menit, detik

  4. Memuat file .sql dari dalam PHP

  5. MySQL dengan Node.js