I have a two sports db tables. I would like to merge both without duplicate entries as defined by one of the columns – $row2[2]', – as shown below. I think I have my code correct, but when I run the script, it just stalls … the program appears to be processing, but the browser timer icon just spins. When I refresh the database, no rows between the two tables have been updated or copied.
I figured that as the script ran, it would more or less update the table automatically, and these updates would be visible upon refresh.
Any help would be greatly appreciated.
<?php
require_once('config.php');
// Give the script unlimited time to run
set_time_limit(0);
// Report all errors except E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);
// Set Error Handler treat Errors as Exceptions
set_error_handler("exception_error_handler");
// Check if Form is Submitted or not
if(isset($_POST["submit"])) {
// Read all rows from second table (Source / appended table containing many duplicate rows)
$sql2 = "SELECT * FROM `".$DB_TABLE_NAME_2."`";
// Execute query and get result set
$result2=mysql_query($sql2);
// Check for result
if($result2) {
// Get number of rows found
$num_rows2=mysql_num_rows($result2);
// Check number of rows
if($num_rows2>0) {
// Loop each row from the result set
while($row2=mysql_fetch_array($result2)) {
// Find duplicate data in first table (Destination / Original table containing unique rows).
$sql1 = "SELECT `".$DB_TABLE_VOTER_BASE_COLUMN_NAME // Column from Destination File
."` FROM `".$DB_TABLE_NAME_1 // Destination File
."` WHERE `".$DB_TABLE_VOTER_BASE_COLUMN_NAME // Column from Destination File
."` = '".$row2[$DB_TABLE_VOTER_BASE_COLUMN_NAME]."'"; // Column from Source File
// Execute query and get result set
$result1=mysql_query($sql1);
// Check for result
if($result1) {
// Get number of rows found
$num_rows1=mysql_num_rows($result1);
// Check number of rows
if($num_rows1==0) {
// Add new record into first table from second table
$sql =
"
INSERT INTO `($DB_TABLE_NAME_1)`
(
`id`,
`ranker`,
`referee`,
`position`,
`refereeStyle`,
`baseLoad`,
`baseCount`,
`baseAverage`,
`eraClass`,
`refereeTraining`
)
VALUES
(
'$row2[0]',
'$row2[1]',
'$row2[2]',
'$row2[3]',
'$row2[4]',
'$row2[5]',
'$row2[6]',
'$row2[7]',
'$row2[8]',
'$row2[9]'
)
"
;
// Execute insert query
mysql_query($sql);
}
}
// wait for 500 milliseconds (0.5 Sec)
waitFor(500);
}
}
}
echo "<br>Completed.<br><a href='index.php'>More.</a>";
} else {
header('Location: index.php'); //Redirect to index.php if Form not submitted
}
// Function to Delays program execution for the given number of milliseconds.
function waitFor($s) {
usleep($s*1000); // Value in microseconds
}
// Function to handle Errors
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
// Throw new Exception on Error
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
?>