#!/usr/bin/php
<?php
  $dbhost = "localhost"; // change this value
  $dbname = "fboard";    // change this value
  $dbuser = "user";      // change this value
  $dbpass = "password";  // change this value


  mysql_connect($dbhost, $dbuser, $dbpass)
    or die("Could not connect");
  mysql_select_db($dbname)
    or die("Could not access database.");

  $query1 = "SELECT tid,MAX(date) AS newdate
            FROM f_board_message 
            GROUP BY tid";

  $result = mysql_query($query1)
    or die("Could not execute select query.");

  while($row = mysql_fetch_array($result)) {
    $tid = $row['tid'];
    $newdate = $row['newdate'];

    $query2 = "UPDATE f_board_thread 
               SET moddate = '$newdate' 
               WHERE tid = $tid AND moddate <> '$newdate'";

    mysql_query($query2)
      or die("Could not execute update query.");
  }

  mysql_free_result($result);
  mysql_close();
?>
