<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "PHP Newbie - Update Scripts"]]></title>
		<link>http://forum.geonames.org/gforum/posts/list/4.page</link>
		<description><![CDATA[Latest messages posted in the topic "PHP Newbie - Update Scripts"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>PHP Newbie - Update Scripts</title>
				<description><![CDATA[ Hi,

I'm currently trying to write some php scripts to utilise the "update" files available in the download section to keep a mysql copy of the geonames dataset in synch.

So far I've got one that pretty much works on the "modifications" file - code herewith (currently doing an delete and insert instead of update until I'm ready to load the full file):

<span class="genmed"><b>Code:</b></span><br>
		<div style="overflow: auto; width: 100%;">
		<pre>
&lt;?php
  date_default_timezone_set&#40;"UTC"&#41;;

  $mysqlConnection = mysql_connect&#40;"server","db","pwd"&#41;;

  if &#40;!$mysqlConnection&#41;
    {
      die&#40;"Could not connect: " . mysql_error&#40;&#41;&#41;;
    }

  mysql_set_charset&#40;"utf8"&#41;;

  mysql_select_db&#40;"db"&#41;;

mysql_query&#40;"DELETE FROM geonames"&#41;;

  // Get a file into an array.
  $lines = file&#40;"http://download.geonames.org/export/dump/modifications-" . date&#40;"Y-m-d", time&#40;&#41; - 86400&#41; . ".txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES&#41;;

  // Loop through our array.
  foreach &#40;$lines as $line_num =&gt; $line&#41;
    {
      $lineData = explode&#40;"\t", $line&#41;;
print 'INSERT INTO geonames VALUES &#40;"' . $lineData&#91;0&#93; . '", "' . $lineData&#91;1&#93; . '", "' . $lineData&#91;2&#93; . '", "' . $lineData&#91;3&#93; . '", "' . $lineData&#91;4&#93; . '", "' . $lineData&#91;5&#93; . '", "' . $lineData&#91;6&#93; . '", "' . $lineData&#91;7&#93; . '", "' . $lineData&#91;8&#93; . '", "' . $lineData&#91;9&#93; . '", "' . $lineData&#91;10&#93; . '", "' . $lineData&#91;11&#93; . '", "' . $lineData&#91;12&#93; . '", "' . $lineData&#91;13&#93; . '", "' . $lineData&#91;14&#93; . '", "' . $lineData&#91;15&#93; . '", "' . $lineData&#91;16&#93; . '", "' . $lineData&#91;17&#93; . '", "' . $lineData&#91;18&#93; . '"&#41;' . "&lt;br /&gt;";
      mysql_query&#40;'INSERT INTO geonames VALUES &#40;"' . $lineData&#91;0&#93; . '", "' . $lineData&#91;1&#93; . '", "' . $lineData&#91;2&#93; . '", "' . $lineData&#91;3&#93; . '", "' . $lineData&#91;4&#93; . '", "' . $lineData&#91;5&#93; . '", "' . $lineData&#91;6&#93; . '", "' . $lineData&#91;7&#93; . '", "' . $lineData&#91;8&#93; . '", "' . $lineData&#91;9&#93; . '", "' . $lineData&#91;10&#93; . '", "' . $lineData&#91;11&#93; . '", "' . $lineData&#91;12&#93; . '", "' . $lineData&#91;13&#93; . '", "' . $lineData&#91;14&#93; . '", "' . $lineData&#91;15&#93; . '", "' . $lineData&#91;16&#93; . '", "' . $lineData&#91;17&#93; . '", "' . $lineData&#91;18&#93; . '"&#41;'&#41;;
    }

  mysql_close&#40;$mysqlConnection&#41;;
?&gt;
</pre>
		</div>

I'm actually quite happy with this effort so far since its the first php script I've ever written!   :) 

Before I start replicating this for the other "update" files can you please advise how this should be improved?

For instance I'm sure there should be more error checking in there (e.g. for when the update file isn't there and the geonames server returns a 302) or when the db connection doesn't happen etc but I finding it hard to work my way through the php manual (missing the mysql_set_charset had me scratching my noggin for quite some time I can tell you  :oops: ) and aren't really sure what I should be looking at.  Also I'd like to add some sort of auto email function if it fails.

As you can probably guess I want to make it as "bullet proof" as is reasonably possible - can anyone offer my some advice?

Thanks,


Adam M.]]></description>
				<guid isPermaLink="true">http://forum.geonames.org/gforum/posts/list/1673.page#6908</guid>
				<link>http://forum.geonames.org/gforum/posts/list/1673.page#6908</link>
				<pubDate><![CDATA[Wed, 30 Dec 2009 00:04:28]]> GMT</pubDate>
				<author><![CDATA[ admoss1980]]></author>
			</item>
			<item>
				<title>Re:PHP Newbie - Update Scripts</title>
				<description><![CDATA[ Ok... so digging a bit further it looks like I can incorporate the email() function to do the email bit.  I would appreciate some advice on dealing with the 302 and additional error checking though.

Thanks.]]></description>
				<guid isPermaLink="true">http://forum.geonames.org/gforum/posts/list/1673.page#6918</guid>
				<link>http://forum.geonames.org/gforum/posts/list/1673.page#6918</link>
				<pubDate><![CDATA[Sat, 2 Jan 2010 00:21:56]]> GMT</pubDate>
				<author><![CDATA[ admoss1980]]></author>
			</item>
	</channel>
</rss>