gpt4 book ai didi

php - 如何在使用准备好的语句进行查询期间使用 PHP 填充数组

转载 作者:行者123 更新时间:2023-11-29 13:14:43 24 4
gpt4 key购买 nike

我将在这里发布整个 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 和英雄名称)放入数组中以供稍后使用创建图表。

这是更集中的部分:

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 :无法在第 79 行的 C:\xampp\htdocs\Dota2HomePage\d2index.php 中使用 mysqli_stmt 类型的对象作为数组

我想我做错了什么!

最佳答案

您应该使用绑定(bind)变量:

while($stmt -> fetch()){
...
$grapharray[$hero] = $mmr;
}

关于php - 如何在使用准备好的语句进行查询期间使用 PHP 填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21624337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com