我如何循环通过mysql?

时间:2011-01-27 14:20:14

标签: php mysql

我有一个100k域及其标签的列表。标签用逗号分隔。

由于UTF编码问题,某些标签是奇怪的字符。所以我想摆脱这个。

所以我想遍历我的数据库中的每个域 然后爆炸标签然后从不需要的字符过滤它 然后将其插入新表中。

我的桌子看起来像这样......

    Domain          Tags
    yahoo.co.jp     yahoo,search,portal,japan,ãƒãƒ¼ã‚¿ãƒ«,検索,news
    163.com         news,门户,æ–°é—»,portal,网易,163,china,门户ç 
    qq.com          qq,china,portal,news,im,门户,chinese,web,socialn...

2 个答案:

答案 0 :(得分:-1)

$query = "SELECT * FROM [tablename];";
$result  = mysql_query($query);
while($r = mysql_fetch_array($result)
{
   $tags = explode($r["Tags"]);
   //Do your work here
   $newtags = FilterTags(",", $tags);
   $tagstring = implode(",", $newtags);
   $query = "INSERT INTO [NewTableName] (`Domain`, `Tags`) VALUES (\"".$r["Domain"]."\", \"".$tagstring."\")";
   mysql_query($query);
}

答案 1 :(得分:-2)

$query = mysql_query("SELECT * FROM mytable");

while ($row = mysql_fetch_array($query)) {

    $cleanstr = mysql_real_escape_string(cleanChars($row['Tags']));
    mysql_update("UPDATE mytable SET Tags = '$cleanstr' WHERE Domain = '$row[\'Domain\']");

}

function cleanChars($str) {
    //this function will explode the string and clean the chars
    //use implode after you clean the unwanted characters to convert the array to a string.
    return $cleanstr;
}