本文實例講述了PHP查詢並刪除數據庫多列重復數據的方法。分享給大家供大家參考,具體如下:
<?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);
?>
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。