PHP查詢并刪除數(shù)據(jù)庫多列重復(fù)數(shù)據(jù)的方法(利用數(shù)組函數(shù)實現(xiàn))
本文實例講述了PHP查詢并刪除數(shù)據(jù)庫多列重復(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); ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
POSIX 風(fēng)格和兼容 Perl 風(fēng)格兩種正則表達(dá)式主要函數(shù)的類比(preg_match, preg_replace,
POSIX 風(fēng)格和兼容 Perl 風(fēng)格兩種正則表達(dá)式主要函數(shù)的類比(preg_match, preg_replace, ereg, ereg_replace) ,需要的朋友可以參考下。2010-10-10ubuntu下編譯安裝xcache for php5.3 的具體操作步驟
本篇文章是對ubuntu下編譯安裝xcache for php5.3的操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06攻克CakePHP系列一 連接MySQL數(shù)據(jù)庫
請先參閱以前寫的文章以便對CakePHP有所了解文章,上一篇2008-10-10