我有以下脚本:
<?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";
?>
当我运行它时,我希望它能够遍历每个位置,并且在每个位置,我希望它从$pointsvar
到达500,直到达到20000.它一直工作,直到我通过每个位置一个while循环现在只输出Inserted 0
我已经增加了max_execution_time,因为它可能需要一段时间才能运行。这个脚本将在每天或每周围绕一个cron运行。
预期输出为Inserted
,数字非常大......
感谢您提供的任何帮助:D
答案 0 :(得分:2)
使用while
循环代替$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";
循环,以确保每次都能正确初始化变量。
foreach ($data['clanList'] as $clan)
此外,也许最里面的循环应该是clanList
- $data['results']
数组中的部落数量是否可以与INSERT
不同?
您可以通过使用单个查询插入多行来加快INSERT INTO tablename (columns...) VALUES (...), (...), (...), ...
次查询:
$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);
因此,在您的脚本中,您可以在循环结束时连接所有值,然后在该循环结束时插入该批处理。所以它看起来像:
{{1}}
答案 1 :(得分:0)
第二个循环完成后,您没有重置$pointsvar = 50000;
。我希望第二个循环因此只在第一次循环外循环时运行。所以
<?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";
?>