Hướng dẫn how copy data from one database to another in php? - Làm thế nào để sao chép dữ liệu từ cơ sở dữ liệu này sang cơ sở dữ liệu khác trong php?

Dưới đây là mã để di chuyển dữ liệu betwee hai cơ sở dữ liệu mà nó kiểm tra xem bảng hoặc cột tồn tại othewise tạo nó và chèn hoặc dữ liệu udpate Lưu ý Nó không phải là giải pháp phù hợp cho cơ sở dữ liệu lớn và đảm bảo bạn bakup cho cơ sở dữ liệu đích của bạn.

mysqli_select_db[$dblink1,'db1'];  // select database 1

$dblink2=mysqli_connect['127.0.0.1', 'root', '']; // connect server 2   

mysqli_select_db[$dblink2,'db2']; // select database 2

$result = [mysqli_query[$dblink1,"SHOW TABLES  "]];
while [$row = mysqli_fetch_row[$result]] {
    $tables[] = $row[0];
}
echo "Searching Table: ".$tables[0] ."
"; foreach [$tables as $i => $table]{ $val = mysqli_query[$dblink2,"select 1 from $table "]; if[$val !== FALSE] /***** table exists */ { $structure1= get_colums[$dblink1, $table]; $structure2= get_colums[$dblink2, $table]; compare_colums[$dblink1,$dblink2, $structure1, $structure2, $table ]; $result = mysqli_query[$dblink1,"SELECT * FROM $table "]; // select all content echo $table." Updating table.... "."
"; $rowcount=mysqli_num_rows[$result]; while [$row = mysqli_fetch_array[$result, MYSQLI_ASSOC] ] { $update_string = update_query_format[$row ]; mysqli_query[$dblink2,"INSERT INTO $table [".implode[", ",array_keys[$row]]."] VALUES ['".implode["', '",array_values[$row]]."'] ON DUPLICATE KEY UPDATE $update_string "]; // insert one row into new table } echo $table." Updating table Completed.... total updated: $rowcount"."


"; } else { /**** table not exists */ echo $table." Table not found "."
"; echo $table." Creating table... "."
"; $tableinfo = mysqli_fetch_array[mysqli_query[$dblink1,"SHOW CREATE TABLE $table "]]; // get structure from table on server 1 mysqli_query[$dblink2," $tableinfo[1] "]; // use found structure to make table on server 2 $result = mysqli_query[$dblink1,"SELECT * FROM $table "]; // select all content echo $table." Copying table.... "."


"; while [$row = mysqli_fetch_array[$result, MYSQLI_ASSOC] ] { mysqli_query[$dblink2,"INSERT INTO $table [".implode[", ",array_keys[$row]]."] VALUES ['".implode["', '",array_values[$row]]."']"]; // insert one row into new table } } } mysqli_close[$dblink1]; mysqli_close[$dblink2]; function compare_structure[$table, $dblink1 , $dblink2]{ /*** Check if table exists */ $val = mysqli_query[$dblink2,"select 1 from $table "]; if[$val !== FALSE] /***** table exists */ { $structure1= get_colums[$dblink1, $table]; $structure2= get_colums[$dblink2, $table]; compare_colums[$dblink1,$dblink2, $structure1, $structure2, $table ]; $result = mysqli_query[$dblink1,"SELECT * FROM $table "]; // select all content echo $table." Updating table.... "."
"; $rowcount=mysqli_num_rows[$result]; while [$row = mysqli_fetch_array[$result, MYSQLI_ASSOC] ] { $update_string = update_query_format[$row ]; mysqli_query[$dblink2,"INSERT INTO $table [".implode[", ",array_keys[$row]]."] VALUES ['".implode["', '",array_values[$row]]."'] ON DUPLICATE KEY UPDATE $update_string "]; // insert one row into new table } echo $table." Updating table Completed.... total updated: $rowcount"."


"; } else { /**** table not exists */ echo $table." Table not found "."
"; echo $table." Creating table... "."
"; $tableinfo = mysqli_fetch_array[mysqli_query[$dblink1,"SHOW CREATE TABLE $table "]]; // get structure from table on server 1 mysqli_query[$dblink2," $tableinfo[1] "]; // use found structure to make table on server 2 $result = mysqli_query[$dblink1,"SELECT * FROM $table "]; // select all content echo $table." Copying table.... "."


"; while [$row = mysqli_fetch_array[$result, MYSQLI_ASSOC] ] { mysqli_query[$dblink2,"INSERT INTO $table [".implode[", ",array_keys[$row]]."] VALUES ['".implode["', '",array_values[$row]]."']"]; // insert one row into new table } } } function get_colums[$dblink, $table]{ $result = mysqli_query[$dblink,"SHOW COLUMNS FROM $table "]; $columns = []; if [!$result] { echo 'Could not run query: ' . mysql_error[]; return []; } if [mysqli_num_rows[$result] > 0] { while [$row = mysqli_fetch_assoc[$result]] { $columns[$row['Field']] = $row['Type']; } } return $columns; } function compare_colums[ $dblink1, $dblink2, $structure1, $structure2, $table ]{ foreach [$structure2 as $field => $type]{ if [!array_key_exists[$field,$structure2]]{ $result = mysqli_query[$dblink2,"ALTER TABLE cus_tbl ADD $field $type NOT NULL "]; if [$result]{ echo "Table: ". $table." New Field created ".$field. " ". $type. " .... "."
"; } } } } function update_query_format[$row]{ $query_format = ""; foreach [$row as $k => $v]{ $query_format .= " ".$k. " = '". $v ."',"; } // echo strlen[$query_format];exit; // echo substr[$query_format, 0,strlen[$query_format]-1]; exit; return substr[$query_format, 0, strlen[$query_format]-1]; }

Bạn có thể sao chép dữ liệu từ cơ sở dữ liệu này sang cơ sở dữ liệu khác không?

Người dùng có thể xuất từ ​​cơ sở dữ liệu nguồn hoặc nhập từ cơ sở dữ liệu đích để sao chép dữ liệu. Các bước cần được tuân thủ là: Khởi chạy SQL Server Management Studio. Chọn và nhấp chuột phải vào cơ sở dữ liệu nguồn, chuyển sang tác vụ> Xuất dữ liệu.. Steps that need to be followed are: Launch SQL Server Management Studio. Select and right-click on the Source Database, go to Tasks > Export Data.

Làm cách nào để sao chép một bảng từ cơ sở dữ liệu này sang cơ sở dữ liệu khác trong PHP?

3] Cuộn xuống phần cơ sở dữ liệu và chọn phpmyadmin.4] Sử dụng cây điều hướng ở thanh bên trái để xác định vị trí bảng cơ sở dữ liệu bạn muốn sao chép.5] Chọn tab Hoạt động ở đầu màn hình.6] Cuộn xuống trang để sao chép bảng vào [cơ sở dữ liệu.

Làm cách nào để sao chép dữ liệu bảng từ cơ sở dữ liệu này sang cơ sở dữ liệu khác trong MySQL?

Cách nhanh nhất để sao chép một bảng trong MySQL:..
Nhấp chuột phải vào bảng bạn muốn sao chép trong cơ sở dữ liệu Explorer và chọn đối tượng trùng lặp ..
Trong hộp thoại mở ra, chọn đích DB ..
Chọn để sao chép dữ liệu bảng hoặc cấu trúc chỉ ..
Chỉ định tên của bảng mới và bấm OK ..

Làm thế nào tôi có thể sao chép cơ sở dữ liệu từ cơ sở dữ liệu này sang cơ sở dữ liệu khác trong phpmyadmin?

Chọn cơ sở dữ liệu bạn muốn sao chép [bằng cách nhấp vào cơ sở dữ liệu từ màn hình chính PhpMyAdmin].Khi ở trong cơ sở dữ liệu, chọn tab Hoạt động.Cuộn xuống phần nơi nó nói cơ sở dữ liệu sao chép vào.Nhập tên của cơ sở dữ liệu mới.

Bài Viết Liên Quan

Chủ Đề