Results 1 to 5 of 5

Thread: Convert playerstats from scriptdata to mysql table

  1. #1
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,003
    Thanks
    439
    Thanked 166 Times in 128 Posts

    Convert playerstats from scriptdata to mysql table

    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:
    Code:
    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 Code:
    <?php

    define
    ('COLUMN_DELIMETER'',');

    $mysqli = new mysqli(MYSQL_HOSTMYSQL_DBUSERMYSQL_DBPASSMYSQL_DBNAMEMYSQL_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_DELIMETERfgets($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);

  2. The Following User Says Thank You to Ni3ls For This Useful Post:

    kung foo man (7th May 2019)

  3. #2
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,592
    Thanks
    17
    Thanked 937 Times in 583 Posts
    Use $file and get a substring out of it.
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

    Sending me a private message makes me want to help you LESS.

  4. The Following User Says Thank You to IzNoGoD For This Useful Post:

    kung foo man (7th May 2019)

  5. #3
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    1,962
    Thanks
    1,886
    Thanked 1,061 Times in 738 Posts
    Had the same problem once, you might be able to pick what you need:

    PHP Code:

    <?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($filename0, -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";
    timescale 0.01

  6. The Following User Says Thank You to kung foo man For This Useful Post:

    Ni3ls (8th May 2019)

  7. #4
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,003
    Thanks
    439
    Thanked 166 Times in 128 Posts
    PHP Code:
    PHP Warning:  file_get_contents(player_56860.txt): failed to open streamNo such file or directory in /home/HIGHJUMP/stats.php on line 24 
    I get this for every file.

    PHP Code:
        <?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";

  8. #5
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,592
    Thanks
    17
    Thanked 937 Times in 583 Posts
    file_get_contents('/home/.callofduty2/sd2/scriptdata/stats/' .$filename)
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

    Sending me a private message makes me want to help you LESS.

  9. The Following User Says Thank You to IzNoGoD For This Useful Post:

    Ni3ls (9th May 2019)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •