How can data from a database query be transferred to another database in PHP?
To transfer data from one database to another in PHP, you can retrieve the data from the source database using a database query, then insert that data into the destination database using another query. This can be done by connecting to both databases, fetching the data from the source database, and then inserting it into the destination database.
// Connect to source database
$sourceConnection = new mysqli('source_host', 'source_username', 'source_password', 'source_database');
// Connect to destination database
$destinationConnection = new mysqli('destination_host', 'destination_username', 'destination_password', 'destination_database');
// Retrieve data from source database
$sourceData = $sourceConnection->query('SELECT * FROM source_table');
// Insert data into destination database
while($row = $sourceData->fetch_assoc()) {
$destinationConnection->query("INSERT INTO destination_table (column1, column2) VALUES ('".$row['column1']."', '".$row['column2']."')");
}
// Close connections
$sourceConnection->close();
$destinationConnection->close();