Home > Backend Development > PHP Tutorial > PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)_PHP

PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)_PHP

WBOY
Release: 2016-05-28 11:48:44
Original
827 people have browsed it

本文实例讲述了PHP查询并删除数据库多列重复数据的方法。分享给大家供大家参考,具体如下:

<&#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);
&#63;>

Copy after login

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

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