PHP world data > mysql

DeletedUser

Guest
Not seen this posted so here is the first working draft.
This is written on the fly so no comments and nothing nice about the script.
crude and working on my shared hosting with cURL. Other MySQL imports didnt work on my cheap hosting so stuck with simple INSERT's.

The first section is purely for caching the API data from grepolis's servers to increase speed and reduce overheads I use this a lot else where. Some hosts and API providers would temp. block you if you kept hammering for the data.

PHP script to insert world data into a MySQL database.

PHP:
<?php
$cachedir="/host_directory/";

if (readCache($cachedir . 'players.gz', 86400) != 2) { /* 1 day */
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,'http://en3.grepolis.com/data/players.txt.gz');
	$fp = fopen('players.gz', 'w');
	curl_setopt($ch, CURLOPT_FILE, $fp);
	curl_exec ($ch);
	curl_close ($ch);
	fclose($fp);
	uncompress("players.gz","players.txt");
	
	$count=0;

	include("dbinfo.inc.php");
	mysql_connect($db_host,$db_user,$db_pwd);
	@mysql_select_db($db_base) or die( "Unable to select database"); 

	$query = "TRUNCATE TABLE ".$tableT1;
	$query = @mysql_query($query);

	$handle = fopen ('./players.txt', 'r');
	while (($data = fgetcsv($handle, 1000, ',', '"')) !== FALSE)
	{
	if ($count == 1000) {
		$query = @mysql_query($query);
		$count = 0;
		}
	$count++;
	if ($count == 1) {
		$query = "INSERT INTO ".$tableT1." VALUES ('". implode("','", $data) ."')";
		} else {
		$query .= ",('". implode("','", $data) ."')";
		}
	}

	$query = @mysql_query($query);

	$query  = "SELECT * FROM `".$tableT1."`";
	$thing = mysql_query($query);
	echo "mysql row count = ".mysql_numrows($thing)."<br>";


	mysql_close();


	}
	

echo "players.txt line count = ".countLines($cachedir.'players.txt')."<br>";



function countLines($filepath)  {
	$handle = fopen( $filepath, "r" );
	$count = 0;
	while( fgets($handle) ) 
	{
		$count++;
	}
	fclose($handle);
	return $count;
}


function readCache($filename, $expiry) {
	if (file_exists($filename)) {  
		if ((time() - $expiry) > filemtime($filename)) { return 1; }
		return 2;
		}
	return 0;
}


function uncompress($srcName, $dstName) {
	$string = implode("", gzfile($srcName));
	$fp = fopen($dstName, "w");
	fwrite($fp, $string, strlen($string));
	fclose($fp);
} 


?>


include("dbinfo.inc.php");
PHP:
<?php
$db_host = 'localhost';
$db_user = 'user';
$db_pwd = 'password';
$db_base = 'database';
$tableT1 = 'table';
?>
 
Last edited by a moderator:

DeletedUser

Guest
The SQL for the table would look something like this

Code:
CREATE TABLE `database`.`table` (
  `id` int(11) NOT NULL,
  `name` varchar(128) NOT NULL,
  `alliance_id` int(11) NOT NULL,
  `points` int(11) NOT NULL,
  `rank` int(11) NOT NULL,
  `towns` int(11) NOT NULL
);
 

DeletedUser

Guest
question...lol?

je vient de fair sa...mais jai une errore sur la page:

mysql row count = 47788

Warning: fopen(/host_directory/players.txt) [function.fopen]: failed to open stream: No such file or directory in /home/asterix0/public_html/PHP/database_connect.php on line 56

Warning: fgets(): supplied argument is not a valid stream resource in /home/asterix0/public_html/PHP/database_connect.php on line 58

Warning: fclose(): supplied argument is not a valid stream resource in /home/asterix0/public_html/PHP/database_connect.php on line 62
players.txt line count = 0

alors que ma database est bien remplie des donnees...

que doit faire?
 

DeletedUser5

Guest
You don't have the file "players.txt" in your "host_directory". Have you downloaded the file, or do you just expect it to be there?
 

DeletedUser

Guest
You don't have the file "players.txt" in your "host_directory". Have you downloaded the file, or do you just expect it to be there?

il faut que je telecharge une file ? players.txt?
je croyait que je devait juste en creer une appeler players.txt...
se que jai fait...lol
en plus elle a prit les datas...
ma database est remplis aussi des donnees des players....
sa a marcher...mais jai cette errore la..que je comprend pas...
pfffffffffff
encore beaucoupde chose a apprendre..lol:eek:
 

DeletedUser

Guest
oups..i didnt realized that i was on the english forum...lol
my apologize...

did you understand my bad french?
 

DeletedUser

Guest
so i was sayng that the file players.txt, i did created it and is in the same directory on my host.
but apart those errores, my database get filled and i can do search,browse in phpadmin..
 
Top