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();
?>