I am trying to trim apartment numbers off of an address field in a 800,000 record PostgreSQL table. It works for only 16 times through the for...loop then quits even though the results of the SELECT statement is 25,000. So logically it should run through the iteration 25,00 times. How come it craps out after 16 iterations of the for...loop?
Here is the script and result:
PHP Script
<?PHP
// Connect to PostgreSQL.
$db = pg_Connect( "host=192.168.41.14 dbname=BC_addresses user=postgres password=squid" );
$query = "select address, split_part(address,'-',2) as result_split from \"GVRD1\" WHERE \"address\" ilike '%-%' and \"city\" = 'Burnaby';";
$query_count = "SELECT count(address) FROM \"GVRD1\" WHERE \"address\" ilike '%-%' and \"city\" = 'Burnaby';";
$result = pg_Exec( $db, $query );
$result_count = pg_Exec( $db, $query_count );
$row_count = pg_Fetch_Row( $result_count, 0 );
echo "$row_count[0] results returned<br><br>";
// begin loop
for ($ii = 0; $ii < $row_count[0]; $ii++)
{
$row = pg_Fetch_Row( $result, $ii );
$addressResult = $row[0];
$splitaddressResult = $row[1];
$update_sql = "UPDATE \"GVRD1\" SET address = '$row[1]' where address = '$row[0]';";
echo "$row[0] originally - now converted to: <font color=yellow><b>$row[1]</font></b><br>";
$result_update = pg_Exec( $db, $update_sql );
}
pg_Close( $db );
?>
HTML OUTPUT
24899 results returned
304-9140 Halston Crt originally - now converted to: 9140 Halston Crt
2106-6837 Station Hill Dr originally - now converted to: 6837 Station Hill Dr
406-6742 Station Hill Crt originally - now converted to: 6742 Station Hill Crt
56-8701 16th Ave originally - now converted to: 8701 16th Ave
304-9584 Manchester Dr originally - now converted to: 9584 Manchester Dr
12-7128 18th Ave originally - now converted to: 7128 18th Ave
12-8277 11th Ave originally - now converted to: 8277 11th Ave
115-7428 19th Ave originally - now converted to: 7428 19th Ave
503-7151 Edmonds St originally - now converted to: 7151 Edmonds St
905-9603 Manchester Dr originally - now converted to: 9603 Manchester Dr
74-8701 16th Ave originally - now converted to: 8701 16th Ave
214-6745 Station Hill Crt originally - now converted to: 6745 Station Hill Crt
802-6888 Station Hill Dr originally - now converted to: 6888 Station Hill Dr
1607-9633 Manchester Dr originally - now converted to: 9633 Manchester Dr
1009-6837 Station Hill Dr originally - now converted to: 6837 Station Hill Dr
30-7179 18th Ave originally - now converted to: 7179 18th Ave
8-7175 17th Ave originally - now converted to: 7175 17th Ave
Here is the script and result:
PHP Script
<?PHP
// Connect to PostgreSQL.
$db = pg_Connect( "host=192.168.41.14 dbname=BC_addresses user=postgres password=squid" );
$query = "select address, split_part(address,'-',2) as result_split from \"GVRD1\" WHERE \"address\" ilike '%-%' and \"city\" = 'Burnaby';";
$query_count = "SELECT count(address) FROM \"GVRD1\" WHERE \"address\" ilike '%-%' and \"city\" = 'Burnaby';";
$result = pg_Exec( $db, $query );
$result_count = pg_Exec( $db, $query_count );
$row_count = pg_Fetch_Row( $result_count, 0 );
echo "$row_count[0] results returned<br><br>";
// begin loop
for ($ii = 0; $ii < $row_count[0]; $ii++)
{
$row = pg_Fetch_Row( $result, $ii );
$addressResult = $row[0];
$splitaddressResult = $row[1];
$update_sql = "UPDATE \"GVRD1\" SET address = '$row[1]' where address = '$row[0]';";
echo "$row[0] originally - now converted to: <font color=yellow><b>$row[1]</font></b><br>";
$result_update = pg_Exec( $db, $update_sql );
}
pg_Close( $db );
?>
HTML OUTPUT
24899 results returned
304-9140 Halston Crt originally - now converted to: 9140 Halston Crt
2106-6837 Station Hill Dr originally - now converted to: 6837 Station Hill Dr
406-6742 Station Hill Crt originally - now converted to: 6742 Station Hill Crt
56-8701 16th Ave originally - now converted to: 8701 16th Ave
304-9584 Manchester Dr originally - now converted to: 9584 Manchester Dr
12-7128 18th Ave originally - now converted to: 7128 18th Ave
12-8277 11th Ave originally - now converted to: 8277 11th Ave
115-7428 19th Ave originally - now converted to: 7428 19th Ave
503-7151 Edmonds St originally - now converted to: 7151 Edmonds St
905-9603 Manchester Dr originally - now converted to: 9603 Manchester Dr
74-8701 16th Ave originally - now converted to: 8701 16th Ave
214-6745 Station Hill Crt originally - now converted to: 6745 Station Hill Crt
802-6888 Station Hill Dr originally - now converted to: 6888 Station Hill Dr
1607-9633 Manchester Dr originally - now converted to: 9633 Manchester Dr
1009-6837 Station Hill Dr originally - now converted to: 6837 Station Hill Dr
30-7179 18th Ave originally - now converted to: 7179 18th Ave
8-7175 17th Ave originally - now converted to: 7175 17th Ave