To transfer data from one table to another using PHP and SQL, you can follow these steps:
Connect to the database: Use the mysqli_connect() function or PDO to connect to the database.
Retrieve data from the source table: Use the SELECT statement to retrieve data from the source table.
Insert data into the target table: Use the INSERT INTO statement to insert the retrieved data into the target table.
Delete data from the source table (optional): Use the DELETE statement to delete the data from the source table after transferring it to the target table.
Example code:
<?php
// Connect to the database
$conn = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Retrieve data from the source table
$sql = "SELECT * FROM source_table";
$result = mysqli_query($conn, $sql);
// Insert data into the target table
$sql = "INSERT INTO target_table (column1, column2, column3) VALUES ";
while ($row = mysqli_fetch_assoc($result)) {
$column1 = $row['column1'];
$column2 = $row['column2'];
$column3 = $row['column3'];
$sql .= "('$column1', '$column2', '$column3'), ";
}
$sql = rtrim($sql, ', ');
mysqli_query($conn, $sql);
// Delete data from the source table (optional)
$sql = "DELETE FROM source_table";
mysqli_query($conn, $sql);
mysqli_close($conn);
?>
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-02-11 11:00:00 +0000
Seen: 11 times
Last updated: Aug 06 '21
What is an Ansible role that includes integrated modules?
How can Yii2 be connected to a remote MySQL database through SSH?
How can records be counted from a category that is retrieved through another PHP and SQL query?
Why is the search function in PHP & MYSQL not providing relevant results?
What is the process to view and make edits to my complete MySQL code?
What is the method for showing errors in mysqli_query?
Is it impossible to generate a composer.lock file within a Docker container?