0

I have the following script:

<?php
ini_set('max_execution_time', 0);
include("../includes/mysqli.php");
$pointsvar = 50000;
$inserts = 0;
$locationvar = 32000006;

while($locationvar <= 32000260){

    while($pointsvar >= 20000){

        $url = "http://185.112.249.77:9999/Api/Search?search=&level=0&min=1&max=50&points=$pointsvar&loc=$locationvar";
        $jsondata = file_get_contents($url);
        $data = json_decode($jsondata, true);

        $in=$data['clanList'];
        $results = $data['results']; //max is 64
        $i = 0;

        while($i + 1 <= $results){

            $clanid = $in[$i]['id'];
            $clanname = mysqli_real_escape_string($con,$in[$i]['name']);
            $clanplayerCount = $in[$i]['playerCount'];
            $clanwarswon = $in[$i]['warsWon'];
            $clanwarslost = $in[$i]['warsLost'];
            $clanwarstied = $in[$i]['warsTied'];
            $clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
            $clanlevel = $in[$i]['level'];
            $score = $in[$i]['score'];

            $sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
            VALUES('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";

            mysqli_query($con, $sql);
            $i++;
            $inserts++;
        }
        $pointsvar = $pointsvar-500;
        sleep(1);
    }
    $locationvar++;
    sleep(1);
}
echo "Inserted $inserts";
?>

When I run it I am expecting it to go through each location and in each location I expect it to -500 from the $pointsvar until it reaches 20000. It was working until I made it go through each location in a while loop and now it just outputs Inserted 0

I have increased the max_execution_time as it could possible take a looong time to run. This script will be run on a cron around every day or week.

The expected output would be Inserted and a very very very big number..

Thanks for any help you can provide :D

8
  • 2
    What debugging have you done. If it's saying "Inserted 0" then it's not entering your 3rd while loop. Check your variables are returning what you expect them to and that the conditions are actually passing. Commented Oct 23, 2015 at 9:13
  • @JonStirling it works when I don't have the first while loop and just have locationvar set manually... And I'm sure it is correct so I'm confused. Commented Oct 23, 2015 at 9:16
  • If it worked before you added the while($locationvar <= 32000260){ loop then the issue must be in that newly added code. Remove it and check the code still works, then add the location loop again carefully Commented Oct 23, 2015 at 9:17
  • 2
    You aren't resetting $pointsvar in the loop. So after the first iteration of the outer loop, the while ($pointsvar >= 20000) loop will exit immediately. Commented Oct 23, 2015 at 9:18
  • 1
    Try using for loops instead of while loops, you're less likely to run into problems like that. Commented Oct 23, 2015 at 9:19

2 Answers 2

2

Use for loops instead of while loops to ensure that your variables get initialized properly each time.

$inserts = 0;
for ($locationvar = 32000006; $locationvar <= 32000260; $locationvar++){

    for ($pointsvar = 50000; $pointsvar >= 20000; $pointsvar -= 500){

        $url = "http://185.112.249.77:9999/Api/Search?search=&level=0&min=1&max=50&points=$pointsvar&loc=$locationvar";
        $jsondata = file_get_contents($url);
        $data = json_decode($jsondata, true);

        $in=$data['clanList'];
        $results = $data['results']; //max is 64

        for ($i = 0; $i < $results; $i++){

            $clanid = $in[$i]['id'];
            $clanname = mysqli_real_escape_string($con,$in[$i]['name']);
            $clanplayerCount = $in[$i]['playerCount'];
            $clanwarswon = $in[$i]['warsWon'];
            $clanwarslost = $in[$i]['warsLost'];
            $clanwarstied = $in[$i]['warsTied'];
            $clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
            $clanlevel = $in[$i]['level'];
            $score = $in[$i]['score'];

            $sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
                    VALUES('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";

            mysqli_query($con, $sql);
            $inserts++;
        }
        sleep(1);
    }
    sleep(1);
}
echo "Inserted $inserts";

Also, maybe the innermost loop should be foreach ($data['clanList'] as $clan) -- can the number of clans in the clanList array be different from $data['results']?

And you can speed up INSERT queries by inserting multiple rows with a single query:

INSERT INTO tablename (columns...) VALUES (...), (...), (...), ...

So in your script, you could concatenate all the values during the loop over clans, and then insert that batch at the end of that loop. So it would look like:

        $values = array();
        for ($i = 0; $i < $results; $i++){

            $clanid = $in[$i]['id'];
            $clanname = mysqli_real_escape_string($con,$in[$i]['name']);
            $clanplayerCount = $in[$i]['playerCount'];
            $clanwarswon = $in[$i]['warsWon'];
            $clanwarslost = $in[$i]['warsLost'];
            $clanwarstied = $in[$i]['warsTied'];
            $clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
            $clanlevel = $in[$i]['level'];
            $score = $in[$i]['score'];

            $values[] = "('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";
        }
        $sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
                VALUES " . implode(', ', $values);
        mysqli_query($con, $sql);
        $inserts += count($values);
Sign up to request clarification or add additional context in comments.

5 Comments

much more readable this way and no need to wory about reseting conditions or infinite loops
Don't understand what you mean about the queries stuff though it looks interesting. Care to expand? :)
And no, $data['results']; is the same as the number of clans in clanList
I've added that to the answer.
@Barmar testing the script now :D
0

You are not resetting $pointsvar = 50000; when the second loop is completed. I would expect that the second loop would therefore only run on the first time round the outer loop. So

<?php
ini_set('max_execution_time', 0);
include("../includes/mysqli.php");
$pointsvar = 50000;
$inserts = 0;
$locationvar = 32000006;

while($locationvar <= 32000260){

    while($pointsvar >= 20000){

        $url = "http://185.112.249.77:9999/Api/Search?search=&level=0&min=1&max=50&points=$pointsvar&loc=$locationvar";
        $jsondata = file_get_contents($url);
        $data = json_decode($jsondata, true);

        $in=$data['clanList'];
        $results = $data['results']; //max is 64
        $i = 0;

        while($i + 1 <= $results){

            $clanid = $in[$i]['id'];
            $clanname = mysqli_real_escape_string($con,$in[$i]['name']);
            $clanplayerCount = $in[$i]['playerCount'];
            $clanwarswon = $in[$i]['warsWon'];
            $clanwarslost = $in[$i]['warsLost'];
            $clanwarstied = $in[$i]['warsTied'];
            $clanLocation = mysqli_real_escape_string($con,$in[$i]['clanLocation']);
            $clanlevel = $in[$i]['level'];
            $score = $in[$i]['score'];

            $sql = "INSERT INTO activeclans(id, name, location, playercount, clanlevel, warswon, warslost, warstied, score)
            VALUES('$clanid', '$clanname', '$clanLocation', '$clanplayerCount', '$clanlevel', '$clanwarswon', '$clanwarslost', '$clanwarstied', $score)";

            mysqli_query($con, $sql);
            $i++;
            $inserts++;
        }
        $pointsvar = $pointsvar-500;
        sleep(1);
    }
    $locationvar++;

    // RESET The pointsvar counter
    $pointsvar = 50000;

    sleep(1);
}
echo "Inserted $inserts";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.