Home > Backend Development > PHP Tutorial > The method of querying and deleting duplicate data in multiple columns of database using PHP is implemented using array functions

The method of querying and deleting duplicate data in multiple columns of database using PHP is implemented using array functions

WBOY
Release: 2016-07-29 09:02:12
Original
837 people have browsed it

The example in this article describes how to query and delete duplicate data in multiple columns of the database with PHP. Share it with everyone for your reference, the details are as follows:

<&#63;php
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db", $con);
if (!$db_selected)
{
    die ("Can/'t use test_db : " . mysql_error());
}
$sql = "SELECT * FROM friend";
$result=mysql_query($sql,$con);
while($myrow=mysql_fetch_row($result))
{
    $arr_data[$myrow[0]]=$myrow[1]."-".$myrow[2];
}
$arr_unique=array_unique($arr_data);
$arr_rep=array_diff_assoc($arr_data,$arr_unique);
//显示前后量
echo count($arr_data)."-".count($arr_unique);
foreach($arr_rep as $key=>$value){
  $sql_del = "DELETE FROM friend WHERE id = '{$key}' ";
  $result=mysql_query($sql_del,$con);
}
// 一些代码
mysql_close($con);
?>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP String Usage" ", "Introduction Tutorial on PHP+MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

The above introduces the method of PHP querying and deleting duplicate data in multiple columns of the database using array functions, including the following aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template