本文實(shí)例講述了PHP查詢并刪除數(shù)據(jù)庫(kù)多列重復(fù)數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
<?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);?>