<?php

$dbh = new PDO('mysql:host=115.84.178.137;dbname=tvtl', 'violet', 'bk,,123~~', array( PDO::ATTR_PERSISTENT => false));
$stmt = $dbh->prepare("select cg_id, cg_family from tblcategory");
$stmt->execute();

echo "Start process at ".date("Y-m-d h:i:s")."\n";
$numRecord = 0;
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
  list($cg_id, $cg_family) = array_values($row);
  $parents = explode("/", $cg_family);
  for ($i = 0; $i < count($parents); $i++) {
    if ($parents[$i] == $argv[1]) {
      $parents[$i] = $argv[2];
      $cg_family = implode("/", $parents);
      echo "update tblcategory set cg_family='$cg_family' where cg_id=$cg_id;\n";
      $numRecord++;
    }
  }
}

echo "\nTotal $numRecord records finish at ".date("h:i:s")."\n\n";
