spiritssight
01-23-2008, 02:38 PM
Hello Everyone,
I am looking for better ways to do the below script which does:
get count of table (irs_rawdata)
gets first record in the table (irs_rawdata)
checks to see if its in block table if so deletes it from irs_rawdata
if not, then it checks if matches some WHERE rules if match adds to query table, if its in query table already then it updates the record
then loops again to to get the first record again and does this for the full table count.
This script is like the other FILTERING script that I have posted but I had to make a number of changes to it as I was not thing clearly and code a little to much into the first one that was not needed (I got carried away with it)
This script is very slow when it comes to processing over 1,000 records, I need it to be able to handle over 1m.
here is the script:
<?php
include '/home/dev/www/lib/php_header.php';
if(isset($_SESSION[ "user_id" ]) & isset($_SESSION[ "user_access_level" ]))
{
include '/home/dev/www/lib/db_config_cr-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
// Counts record in table
$query_s = "SELECT COUNT(*) FROM irs_rawdata";
$result_s = mysql_query($query_s) OR die("Sorry, unable to count the records in the table!");
$result_c = mysql_result($result_s, 0);
$record_count = $result_c;
echo "There is ". $record_count ." records in the irs_rawdata table<br /><br />";
// checks to see if sesult_c is greater then zero
// if so then it runs script
// otherwise it says there is no more records to process
for($i=1; $i <= $result_c; $i++)
{
//used to change $i to a meanful var name
$record_id = $i;
// selects and limits it to the first row only and retrives the EIN then return a 1 or 0
$query_s = "SELECT EIN FROM irs_rawdata LIMIT 0,1";
$result_s = mysql_query($query_s) OR die("Sorry, unable to to select EIN for the first record! <br /> ". mysql_error());
$record = mysql_fetch_assoc($result_s);
$count = mysql_num_rows($result_s);
if($count)
{
// changes long var to short var
$fin = $record['EIN'];
// gets record if there is one in block returns 1 or 0
echo "We are now processing record ". $record_id. " with Fed ID #". $fin ."!<br />";
$query_s = "SELECT fin FROM block WHERE fin = '$fin'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
$record = mysql_fetch_assoc($result_s);
$count = mysql_num_rows($result_s);
if ($count == 0)
{
echo "There was no record found in the \"block\" table!<br />";
echo "We are now checking if record ". $record_id ." matchs the required criteria!<br />";
$query_is = "INSERT INTO query SELECT * FROM irs_rawdata
WHERE EIN = '$fin' AND
(
PNO LIKE '%blind%' OR
PNO LIKE '%deaf%' OR
PNO LIKE '%braille%' OR
PNO LIKE '%sight%' OR
PNO LIKE '%vision%' OR
PNO LIKE '%sign%' OR
PNO LIKE '%ear%' OR
PNO LIKE '%eye%' OR
PNO LIKE '%dog%' OR
Activity_Code LIKE '%031%' OR
NTEE_Code LIKE '%G41%' OR
NTEE_Code LIKE '%G42%' OR
NTEE_Code LIKE '%H41%' OR
NTEE_Code LIKE '%H42%' OR
NTEE_Code LIKE '%P86%' OR
NTEE_Code LIKE '%P87%'
)";
$result_is = mysql_query($query_is);
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully added the record to the \"query\" table, it match the criteria!<br />";
echo "We are now deleting record ". $record_id . " from \"irs_rawdata\" table!<br />";
$query_d = "DELETE FROM irs_rawdata WHERE EIN = '$fin'";
$result_d = mysql_query($query_d);
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully deleted the record from the \"irs_rawdata\" table!<br /><br />";
}
}
else
{
echo "We was unable to match the record ". $record_id ." to the criteria!<br />";
echo "We are now adding record ". $record_id . " to \"block\" table!<br />";
$query_i = "INSERT INTO block (fin, who_blocked) VALUES ('$fin', 'S-123456789')";
$result_i = mysql_query($query_i) OR die("Sorry was unable to insert ".$fin." into the database table allowed! <br />" . mysql_error());
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully added the record to the \"block\" table!<br />";
echo "We are now deleting record ". $record_id . " from \"irs_rawdata\" table!<br />";
$query_d = "DELETE FROM irs_rawdata WHERE EIN ='$fin'";
$result_d = mysql_query($query_d) OR die("Was unable to delete ".$fin." from the db table irs_rawdata! <br />" . mysql_error());
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully deleted the record from the \"irs_rawdata\" table!<br /><br />";
}
}
else
{
echo "We was unable to successfully added the record to the \"block\" table!<br />";
}
}
}
else
{
echo "There was one record found in the \"block\" table!<br />";
echo "We are now deleting record ". $record_id . " from \"irs_rawdata\" table!<br />";
$query_d = "DELETE FROM irs_rawdata WHERE EIN = '$fin'";
$result_d = mysql_query($query_d) OR die("Was unable delete ".$fin." from the db table irs_rawdata! <br />" . mysql_error());
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully deleted the record from the \"irs_rawdata\" table!<br /><br />";
}
}
}
}
echo "The computer has finished processing the records in irs_rawdata!";
include_once ('/home/dev/www/lib/db_close.php');
}
else
{
echo "You are not allowed on this page!";
}
include '/home/dev/www/lib/php_footer.php';
?>
Thanks for your advice and help with this script! I am learning stuff it may not seem it but I am learning slowly,
Sincerely,
Christopher
I am looking for better ways to do the below script which does:
get count of table (irs_rawdata)
gets first record in the table (irs_rawdata)
checks to see if its in block table if so deletes it from irs_rawdata
if not, then it checks if matches some WHERE rules if match adds to query table, if its in query table already then it updates the record
then loops again to to get the first record again and does this for the full table count.
This script is like the other FILTERING script that I have posted but I had to make a number of changes to it as I was not thing clearly and code a little to much into the first one that was not needed (I got carried away with it)
This script is very slow when it comes to processing over 1,000 records, I need it to be able to handle over 1m.
here is the script:
<?php
include '/home/dev/www/lib/php_header.php';
if(isset($_SESSION[ "user_id" ]) & isset($_SESSION[ "user_access_level" ]))
{
include '/home/dev/www/lib/db_config_cr-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
// Counts record in table
$query_s = "SELECT COUNT(*) FROM irs_rawdata";
$result_s = mysql_query($query_s) OR die("Sorry, unable to count the records in the table!");
$result_c = mysql_result($result_s, 0);
$record_count = $result_c;
echo "There is ". $record_count ." records in the irs_rawdata table<br /><br />";
// checks to see if sesult_c is greater then zero
// if so then it runs script
// otherwise it says there is no more records to process
for($i=1; $i <= $result_c; $i++)
{
//used to change $i to a meanful var name
$record_id = $i;
// selects and limits it to the first row only and retrives the EIN then return a 1 or 0
$query_s = "SELECT EIN FROM irs_rawdata LIMIT 0,1";
$result_s = mysql_query($query_s) OR die("Sorry, unable to to select EIN for the first record! <br /> ". mysql_error());
$record = mysql_fetch_assoc($result_s);
$count = mysql_num_rows($result_s);
if($count)
{
// changes long var to short var
$fin = $record['EIN'];
// gets record if there is one in block returns 1 or 0
echo "We are now processing record ". $record_id. " with Fed ID #". $fin ."!<br />";
$query_s = "SELECT fin FROM block WHERE fin = '$fin'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
$record = mysql_fetch_assoc($result_s);
$count = mysql_num_rows($result_s);
if ($count == 0)
{
echo "There was no record found in the \"block\" table!<br />";
echo "We are now checking if record ". $record_id ." matchs the required criteria!<br />";
$query_is = "INSERT INTO query SELECT * FROM irs_rawdata
WHERE EIN = '$fin' AND
(
PNO LIKE '%blind%' OR
PNO LIKE '%deaf%' OR
PNO LIKE '%braille%' OR
PNO LIKE '%sight%' OR
PNO LIKE '%vision%' OR
PNO LIKE '%sign%' OR
PNO LIKE '%ear%' OR
PNO LIKE '%eye%' OR
PNO LIKE '%dog%' OR
Activity_Code LIKE '%031%' OR
NTEE_Code LIKE '%G41%' OR
NTEE_Code LIKE '%G42%' OR
NTEE_Code LIKE '%H41%' OR
NTEE_Code LIKE '%H42%' OR
NTEE_Code LIKE '%P86%' OR
NTEE_Code LIKE '%P87%'
)";
$result_is = mysql_query($query_is);
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully added the record to the \"query\" table, it match the criteria!<br />";
echo "We are now deleting record ". $record_id . " from \"irs_rawdata\" table!<br />";
$query_d = "DELETE FROM irs_rawdata WHERE EIN = '$fin'";
$result_d = mysql_query($query_d);
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully deleted the record from the \"irs_rawdata\" table!<br /><br />";
}
}
else
{
echo "We was unable to match the record ". $record_id ." to the criteria!<br />";
echo "We are now adding record ". $record_id . " to \"block\" table!<br />";
$query_i = "INSERT INTO block (fin, who_blocked) VALUES ('$fin', 'S-123456789')";
$result_i = mysql_query($query_i) OR die("Sorry was unable to insert ".$fin." into the database table allowed! <br />" . mysql_error());
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully added the record to the \"block\" table!<br />";
echo "We are now deleting record ". $record_id . " from \"irs_rawdata\" table!<br />";
$query_d = "DELETE FROM irs_rawdata WHERE EIN ='$fin'";
$result_d = mysql_query($query_d) OR die("Was unable to delete ".$fin." from the db table irs_rawdata! <br />" . mysql_error());
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully deleted the record from the \"irs_rawdata\" table!<br /><br />";
}
}
else
{
echo "We was unable to successfully added the record to the \"block\" table!<br />";
}
}
}
else
{
echo "There was one record found in the \"block\" table!<br />";
echo "We are now deleting record ". $record_id . " from \"irs_rawdata\" table!<br />";
$query_d = "DELETE FROM irs_rawdata WHERE EIN = '$fin'";
$result_d = mysql_query($query_d) OR die("Was unable delete ".$fin." from the db table irs_rawdata! <br />" . mysql_error());
$count = mysql_affected_rows();
if($count)
{
echo "We have successfully deleted the record from the \"irs_rawdata\" table!<br /><br />";
}
}
}
}
echo "The computer has finished processing the records in irs_rawdata!";
include_once ('/home/dev/www/lib/db_close.php');
}
else
{
echo "You are not allowed on this page!";
}
include '/home/dev/www/lib/php_footer.php';
?>
Thanks for your advice and help with this script! I am learning stuff it may not seem it but I am learning slowly,
Sincerely,
Christopher