How can PHP be effectively utilized to compare two tables with specific columns like email addresses?

To compare two tables with specific columns like email addresses in PHP, you can use SQL queries to retrieve the data from both tables and then compare the email addresses. One way to do this is by fetching the email addresses from both tables into arrays and then using PHP functions like array_intersect to find common email addresses.

<?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 email addresses from table1
$sql1 = "SELECT email FROM table1";
$result1 = $conn->query($sql1);
$emails1 = [];
if ($result1->num_rows > 0) {
    while($row = $result1->fetch_assoc()) {
        $emails1[] = $row['email'];
    }
}

// Fetch email addresses from table2
$sql2 = "SELECT email FROM table2";
$result2 = $conn->query($sql2);
$emails2 = [];
if ($result2->num_rows > 0) {
    while($row = $result2->fetch_assoc()) {
        $emails2[] = $row['email'];
    }
}

// Find common email addresses
$common_emails = array_intersect($emails1, $emails2);

// Output common email addresses
echo "Common email addresses: ";
print_r($common_emails);

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