Skip to content

Instantly share code, notes, and snippets.

@jmathai
Created May 18, 2012 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmathai/2727500 to your computer and use it in GitHub Desktop.
Save jmathai/2727500 to your computer and use it in GitHub Desktop.
Fix tag counts in db
<?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']}');");
}
@Daniel15
Copy link

Any particular reason for using the deprecated MySQL library instead of MySQLi or PDO?

@jmathai
Copy link
Author

jmathai commented May 21, 2012

@Daniel15 lazyness. Less to type than using PDO :). Otherwise I use PDO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment