Created
May 18, 2012 20:44
-
-
Save jmathai/2727500 to your computer and use it in GitHub Desktop.
Fix tag counts in db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = '***'; | |
$user = '***'; | |
$pass = '***'; | |
$dbname = '***'; | |
$owner = 'user@example.com'; | |
$conn = mysql_connect($host,$user,$pass); | |
mysql_select_db($dbname); | |
$res = mysql_query("SELECT * FROM photo WHERE owner='{$owner}'"); | |
$allTags = array(); | |
$elementIds = array(); | |
while($row = mysql_fetch_assoc($res)) | |
{ | |
$tags = (array)explode(',', $row['tags']); | |
foreach($tags as $tag) | |
{ | |
$tag = strtolower($tag); | |
if(!isset($allTags[$tag])) | |
$allTags[$tag] = array('public' => 0, 'private' => 0); | |
$allTags[$tag]['private']++; | |
if($row['permission'] == 1) | |
$allTags[$tag]['public']++; | |
$elementIds[] = array('photoId' => $row['id'], 'tag' => $tag); | |
} | |
} | |
mysql_query("DELETE FROM tag WHERE owner='{$owner}'"); | |
foreach($allTags as $tag => $counts) | |
{ | |
mysql_query("INSERT INTO tag(id, owner, countPublic, countPrivate) VALUE('{$tag}', '{$owner}', '{$counts['public']}', '{$counts['private']}');"); | |
} | |
mysql_query("DELETE FROM elementTag WHERE owner='{$owner}'"); | |
foreach($elementIds as $elementTag) | |
{ | |
mysql_query("INSERT INTO elementTag(owner, type, element, tag) VALUE('{$owner}', 'photo', '{$elementTag['photoId']}', '{$elementTag['tag']}');"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any particular reason for using the deprecated MySQL library instead of MySQLi or PDO?