What are some best practices for copying data from one table to another in PHP?

When copying data from one table to another in PHP, it is best practice to use SQL queries to select the data from the source table and insert it into the destination table. This can be done using the INSERT INTO SELECT statement in SQL.

<?php
// Establish connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Copy data from source_table to destination_table
$sql = "INSERT INTO destination_table SELECT * FROM source_table";
if ($conn->query($sql) === TRUE) {
    echo "Data copied successfully!";
} else {
    echo "Error copying data: " . $conn->error;
}

// Close connection
$conn->close();
?>