Tags
Asked 2 years ago
9 Jul 2021
Views 208
Shana

Shana posted

Find duplicate content using MySQL and PHP

Find duplicate content using MySQL and PHP
jaman

jaman
answered Feb 25 '23 00:00

To find duplicate content using MySQL and PHP, you can use the following approach:

Create a database table to store the content you want to check for duplicates. Let's call this table content_table and assume it has two columns: id (an auto-incrementing primary key) and content (the content you want to check for duplicates).

Use PHP to query the content_table and fetch all the records.



<?php

// Establish a connection to the MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database_name");

// Query the database to fetch all records from the content_table
$sql = "SELECT * FROM content_table";
$result = mysqli_query($conn, $sql);

// Loop through the records and store the content in an array
$content_array = array();
while ($row = mysqli_fetch_assoc($result)) {
    $content_array[] = $row['content'];
}

// Close the database connection
mysqli_close($conn);

?>

Use PHP to loop through the content_array and check for duplicate content.


<?php

// Loop through the content array and count the number of occurrences of each item
$occurrences = array_count_values($content_array);

// Loop through the occurrences array and output the duplicates
foreach ($occurrences as $key => $value) {
    if ($value > 1) {
        echo "Duplicate content: " . $key . "<br>";
    }
}

?>

In this example, we use the array_count_values() function to count the number of occurrences of each item in the content_array. We then loop through the occurrences array and output any items that have a count greater than 1.

Post Answer