我将在此处发布整个php,但以下是我遇到的麻烦并着重关注的部分,即数组的实际填充量。
$mysqli = new mysqli('localhost', 'root', '', 'dota_site_test');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$player_id = 123;//change later
if($stmt = $mysqli -> prepare("SELECT Match_ID,Hero,Result,GameMode,MMR FROM matches WHERE Player_ID = ?")) {
$stmt -> bind_param("i", $player_id);
$stmt -> execute();
$stmt -> bind_result($match_id, $hero, $result, $gamemode, $mmr);
//adds the table setup only when data is fetched ok (above)
echo "<table id='matches_table'>";
echo "<thead>
<tr>
<th>Hero</th>
<th>Result</th>
<th>Game Mode</th>
<th>MMR</th>
<th>Diff</th>
</tr>
</thead>";
$i = 1;
$mmr2 = 0;
$grapharray = array();
while($stmt -> fetch()){
//While there is still data being fetched, make the table rows below
$mmr1 = $mmr; //first mmr calc value = mmr from fetch
$diff = 0; //difference defined as 0
if ($mmr2 != 0){ //if the MMR2 has a value (from 2nd loop)
$diff = $mmr1-$mmr2;
}
$mmr2 = $mmr1; //so that both values can be present in the loop
echo "<tr><td>$hero</td>
<td>$result</td>
<td>$gamemode</td>
<td data-id='$i'>$mmr</td>
<td>$diff</td></tr>";
$i++;
$grapharray[$stmt['$hero']] = $stmt['$mmr'];
}
echo "</table>";
$stmt -> close();
}
$mysqli->close();
?>
这是我关注的部分,我试图使用while循环,该循环使用数据填充HTML表,还使用该循环将数据单元格(MMR和Hero名称)放入数组中,以供以后创建图形时使用。
这是更集中的部分:
while($stmt -> fetch()){
//While there is still data being fetched, make the table rows below
$mmr1 = $mmr; //first mmr calc value = mmr from fetch
$diff = 0; //difference defined as 0
if ($mmr2 != 0){ //if the MMR2 has a value (from 2nd loop)
$diff = $mmr1-$mmr2;
}
$mmr2 = $mmr1; //so that both values can be present in the loop
echo "<tr><td>$hero</td>
<td>$result</td>
<td>$gamemode</td>
<td data-id='$i'>$mmr</td>
<td>$diff</td></tr>";
$i++;
$grapharray[$stmt['$hero']] = $stmt['$mmr'];
}
我不确定如何用此循环制作一个数组,因为出现错误:
Fatal error: Cannot use object of type mysqli_stmt as array in C:\xampp\htdocs\Dota2HomePage\d2index.php on line 79
我想我做错了什么!
您应该使用绑定变量:
while($stmt -> fetch()){
...
$grapharray[$hero] = $mmr;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句