when a new block comes in check the database and see what the newest block is
if the database isn't caught up do a loop from the newest block in your database to the newest block in the bitcoin client
example:
for($i = $newest_database_block; $i <= $client_blockcount; $i++)
{
$checkhash = $bitcoin->getblockhash($i);
$block = $bitcoin->getblock($checkhash);
insert block data into database
}
if the database is already caught up grab the last 10 blocks and do a loop in the loop check the hashes
example:
$result = mysql_query("SELECT * FROM `blocks` ORDER BY `id` DESC LIMIT 10");
while($row = mysql_fetch_array($result)) {
$checkblock = $row['id'];
$checkhash = $bitcoin->getblockhash($checkblock);
if($checkhash != $row['hash']){
$block = $bitcoin->getblock($checkhash);
delete old block and put new block in
}
}
this should keep the database up to date and eliminate orphans and split chains