/*------------------------------*/
|
/*
|
Titre : Cherche et trouve les doublons dans une table
|
|
Date édition : 11 Sept 2019
|
Date mise a jour : 11 Sept 2019
|
|
Rapport de la maj:
|
- fonctionnement du code vérifié
|
*/
|
/*------------------------------*/
|
|
/*******************************************************************************
|
* connexion sql + valeur de la table
|
***************************************************************************/
|
|
$db_server = 'localhost'; // Adresse du serveur MySQL
|
$db_name = ''; // Nom de la base de données
|
$db_user_login = 'root'; // Nom de l'utilisateur
|
$db_user_pass = ''; // Mot de passe de l'utilisateur
|
|
// Ouvre une connexion au serveur MySQL
|
$conn = mysqli_connect($db_server,$db_user_login, $db_user_pass, $db_name);
|
|
|
$table_sql = ""; // nom de la table sql
|
$champ_id = ""; // nom de votre identifiant (id,idx...
|
$champ_recherche_sql = ""; // nom du champ sur lequel on effectue la
|
// recherche
|
|
|
/*******************************************************************************
|
* suppression des doublons
|
***************************************************************************/
|
|
if ($_GET['sup'] == 'ok')
|
{
|
$idx = trim($_GET['id']);
|
mysqli_query("DELETE FROM $table_sql WHERE `$table_sql`.`$champ_id`='$idx'");
|
echo '<font color="red">Suppression terminé !!</font> <br />';
|
}
|
|
|
/*******************************************************************************
|
* place tous les champs neccessaires de la table dans un tableau
|
***************************************************************************/
|
|
$tab_match = array();
|
$tab_match_copie = array();
|
$q = $conn->query("SELECT $champ_id,$champ_recherche_sql FROM $table_sql");
|
$i=0;
|
|
$tab_match = array();
|
while ($r = mysqli_fetch_array($q)) {
|
$tab_match[$i] = trim(strtolower($r[$champ_id]));
|
$i++;
|
// Place tous les mots d'une chaine dans un tableau
|
$tab_match[$i] = trim(strtolower($r[$champ_recherche_sql]));
|
$i++;
|
}
|
|
$tab_match_copie = $tab_match;
|
|
|
/*******************************************************************************
|
* l'algo
|
***************************************************************************/
|
|
$i = 1;
|
while ($i < sizeof($tab_match)) {
|
// on prend un champ du tableau
|
$check = $tab_match[$i];
|
$j = 1;
|
$duplicate = 0;
|
while ($j < sizeof($tab_match_copie)) {
|
if ($check == $tab_match_copie[$j]) {
|
$duplicate++;
|
if ($duplicate == 2 AND $tab_match_copie[$j-1] != $tab_match[$i-1]) {
|
echo'duplicate : '.$check.' - id:'.$tab_match_copie[$j-1].' avec '.$tab_match[$i
|
-1].'';
|
echo'<a href="'.$_SERVER['PHP_SELF'].'?sup=ok&id='.$tab_match_copie[$j-1].
|
'">Sup</a>';
|
}
|
}
|
$j =$j + 2;
|
}
|
$i = $i + 2;
|
}
|
| ?> |