PDA

View Full Version : Convert playerstats from scriptdata to mysql table



Ni3ls
6th May 2019, 17:53
I have 80K small .txt files with data which i want to convert to a sql table.

The files in the folder look like this: player_123.txt 123 can be any number but must also be stored in the table as guid

All .txt files have the same layout:
27,1,3,TU VIRGINIDAD,
27 is number of kills 1 is number of headshots 3 is number of bashes TU VIRGINIDAD is the name

My questions :
1. How can you retrieve info from the filename? The $guid in the code below.
2. And how to convert it correctly? I get only zeros now


<?php

define('COLUMN_DELIMETER', ',');

$mysqli = new mysqli(MYSQL_HOST, MYSQL_DBUSER, MYSQL_DBPASS, MYSQL_DBNAME, MYSQL_DBPORT);

if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}


$files = scandir('/home/.callofduty2/sd2/scriptdata/stats');
foreach($files as $file) {

$result = explode(COLUMN_DELIMETER, fgets($file));

$kills = (int)$result[0];
$headshots = (int)$result[1];
$bash = (int)$result[2];
$name = (string)mysqli_real_escape_string($mysqli, $result[3]);
$guid = code to get guid from txt file;

$sql = "INSERT INTO stats (name, kills, headshot, bash, guid) VALUES ('$name', $kills, $headshots, $bash, $guid)";
$mysqli->query($sql);

}

fclose($file);

IzNoGoD
7th May 2019, 20:17
Use $file and get a substring out of it.

kung foo man
7th May 2019, 20:42
Had the same problem once, you might be able to pick what you need:




<?php
echo "scriptdata2mysql by 123123231 29.05.2013\n";

mysql_connect("127.0.0.1:3306", "root", "asd");
mysql_select_db("cod2_test");

$numFiles = 0;
$numStats = 0;
$numErrors = 0;

foreach (glob("*.serverstats") as $filename)
{
$guid = substr($filename, 0, -strlen(".serverstats"));
$content = file_get_contents($filename);

$stats = explode("\n", $content);

foreach ($stats as $stat)
{
$tmp = explode(",", $stat);
$n = count($tmp) . " ";
if ($n != 3)
continue;
$key = $tmp[0];
$value = $tmp[1];
//echo "$n $guid $key => $value\n";

mysql_query("INSERT INTO `stats` (`guid`, `key`, `value`) VALUES ($guid, '$key', '$value')");

if (mysql_error())
{
echo mysql_error() . "\n";
$numErrors++;
}
$numStats++;
}
$numFiles++;
}

echo "Done! Files=$numFiles Stats=$numStats Errors=$numErrors\n";

Ni3ls
8th May 2019, 16:33
PHP Warning: file_get_contents(player_56860.txt): failed to open stream: No such file or directory in /home/HIGHJUMP/stats.php on line 24


I get this for every file.


<?php
echo "scriptdata2mysql by 123123231 29.05.2013\n";
define('MYSQL_HOST', '..');
define('MYSQL_DBNAME', '..');
define('MYSQL_DBUSER', '..);
define('MYSQL_DBPASS', '.');
define('MYSQL_DBPORT', '..');

$mysqli = new mysqli(MYSQL_HOST, MYSQL_DBUSER, MYSQL_DBPASS, MYSQL_DBNAME, MYSQL_DBPORT);

if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

$numFiles = 0;
$numStats = 0;
$numErrors = 0;

$files = scandir('/home/.callofduty2/sd2/scriptdata/stats');
foreach($files as $filename)
{
$guid = substr($filename, 7,-4);
$content = file_get_contents($filename);

$stats = explode("\n", $content);

foreach ($stats as $stat)
{
$tmp = explode(",", $stat);
$n = count($tmp) . " ";
if ($n != 3)
continue;
$kills = $tmp[0];
$headshots = $tmp[1];
$bash = $tmp[2];
$name = $tmp[3];
//echo "$n $guid $key => $value\n";


$sql = "
INSERT INTO
stats
(name, kills, headshot, bash, guid)
VALUES
('$name', '$kills', '$headshots', '$bash', '$guid')
";
$mysqli->query($sql);


if (mysql_error())
{
echo mysql_error() . "\n";
$numErrors++;
}
$numStats++;
}
$numFiles++;
}
echo "Done! Files=$numFiles Stats=$numStats Errors=$numErrors\n";

IzNoGoD
8th May 2019, 18:17
file_get_contents('/home/.callofduty2/sd2/scriptdata/stats/' .$filename)