How can data be transferred from one database to another using PHP on the same server?
To transfer data from one database to another using PHP on the same server, you can establish connections to both databases, retrieve the data from the source database, and insert it into the destination database using SQL queries.
<?php
// Connect to the source database
$sourceConn = new mysqli('localhost', 'source_username', 'source_password', 'source_database');
// Connect to the destination database
$destinationConn = new mysqli('localhost', 'destination_username', 'destination_password', 'destination_database');
// Retrieve data from the source database
$result = $sourceConn->query('SELECT * FROM source_table');
// Insert data into the destination database
while ($row = $result->fetch_assoc()) {
$destinationConn->query("INSERT INTO destination_table (column1, column2) VALUES ('" . $row['column1'] . "', '" . $row['column2'] . "')");
}
// Close connections
$sourceConn->close();
$destinationConn->close();
?>
Keywords
Related Questions
- How does the ternary operator work in PHP and what is its significance in the context of the code snippet provided?
- Are there any performance considerations when using sessions to control IF statement executions in PHP?
- How can the error message "You do not have permission to upload files" be addressed when configuring KCFinder in PHP?