How can PHP be used to fetch data from one table and save it in another table based on a specific value match?

To fetch data from one table and save it in another table based on a specific value match, you can use SQL queries in PHP to retrieve the data from the first table, check for the specific value match, and then insert the data into the second table if the condition is met.

<?php
// Connect 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);
}

// Fetch data from first table based on specific value match
$sql = "SELECT * FROM first_table WHERE specific_column = 'specific_value'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Save fetched data into second table
    while($row = $result->fetch_assoc()) {
        $data_to_save = $row['column_to_save'];
        $sql_insert = "INSERT INTO second_table (column_to_save) VALUES ('$data_to_save')";
        $conn->query($sql_insert);
    }
} else {
    echo "No data found based on specific value match.";
}

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