How can multiple rows be copied from one database table to another in PHP 5 and MySQL 4.1?
To copy multiple rows from one database table to another in PHP 5 and MySQL 4.1, you can use a SELECT query to retrieve the rows from the source table and then INSERT them into the destination table. This can be achieved by executing the SELECT query to fetch the rows and then looping through the result set to insert each row into the destination table.
// Connect to the database
$source_conn = mysqli_connect("localhost", "username", "password", "source_db");
$dest_conn = mysqli_connect("localhost", "username", "password", "dest_db");
// Select rows from the source table
$query = "SELECT * FROM source_table";
$result = mysqli_query($source_conn, $query);
// Loop through the result set and insert rows into the destination table
while ($row = mysqli_fetch_assoc($result)) {
$columns = implode(", ", array_keys($row));
$values = implode("', '", array_values($row));
$insert_query = "INSERT INTO dest_table ($columns) VALUES ('$values')";
mysqli_query($dest_conn, $insert_query);
}
// Close the connections
mysqli_close($source_conn);
mysqli_close($dest_conn);
Keywords
Related Questions
- What are some best practices for ensuring cross-browser compatibility in PHP web development?
- What are the best practices for handling user sessions in PHP applications?
- What are the implications of using functions like mysql_pconnect() in PHP code and why are they considered outdated or not recommended?