我目前正在使用包含一堆用户名的文本文件,而不是我希望我的脚本通过mysql列运行的文本文件。
我遇到麻烦的线路,我想知道我是否可以使用mysql查询的结果作为变量?
$name = file("/var/www/html/memberlist/memberlist.txt");
如果需要,请填写完整的代码
<?php
include 'dbopen.php';
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "Highscores grabber v1.0",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
return $content;
}
function getStats($name)
{
$skills = array('Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints', 'Ranged', 'Prayer', 'Magic', 'Cooking', 'Woodcutting', 'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing', 'Mining', 'Herblore', 'Agility', 'Thieving', 'Slayer', 'Farming', 'Runecrafting', 'Hunter', 'Construction');
$hs = get_web_page("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=$name");
$out = Array();
$stats = explode("\n", $hs);
for($i = 0; $i<count($skills);$i++) {
$stat = explode(',', $stats[$i]);
$out[$skills[$i]] = Array();
$out[$skills[$i]]['rank'] = $stat[0];
$out[$skills[$i]]['level'] = $stat[1];
$out[$skills[$i]]['xp'] = $stat[2];
}
return $out;
}
$name = file("/var/www/html/memberlist/memberlist.txt");
for($j=0;isset($name[$j]);$j++)
{
$out[$j] = getStats($name[$j]);
}
if (!empty($out))
{
for($k=0;isset($out[$k]);$k++)
{
$hitpointsexp = $out[$k]['Hitpoints']['xp'];
$rangedexp = $out[$k]['Ranged']['xp'];
$magicexp = $out[$k]['Magic']['xp'];
$rsn = trim($name[$k]);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$query= ("
INSERT INTO
gains (runescape_name, hitpoints_starting_exp, magic_starting_exp, range_starting_exp)
VALUES
('" . $rsn . "', '" . $hitpointsexp . "', '" . $magicexp . "', '" . $rangedexp . "')
ON DUPLICATE KEY UPDATE
hitpoints_starting_exp='$hitpointsexp', magic_starting_exp='$magicexp', range_starting_exp='$rangedexp'
");
$result = mysql_query($query);
}
}
?>