在使用准备好的语句进行查询期间,如何使用PHP填充数组
我将在这里发布整个php,但下面是我遇到的问题和重点,数组的实际数量在使用准备好的语句进行查询期间,如何使用PHP填充数组,php,mysql,arrays,Php,Mysql,Arrays,我将在这里发布整个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 = $mysql
$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();
?>
$mysqli=newmysqli('localhost','root','dota_site_test');
if(mysqli\u connect\u errno()){
echo“连接失败:”.mysqli_connect_errno();
退出();
}
$player_id=123//改天
如果($stmt=$mysqli->prepare($stmt=$mysqli->prepare)(“从玩家ID=?”的匹配中选择匹配ID、英雄、结果、游戏模式、MMR”){
$stmt->bind_参数(“i”,$player_id);
$stmt->execute();
$stmt->bind_result($match_id、$hero、$result、$gamemode、$mmr);
//仅在数据提取正常(如上)时添加表设置
回声“;
回声“
英雄
结果
游戏模式
嗯
差异
";
$i=1;
$mmr2=0;
$grapharray=array();
而($stmt->fetch()){
//当仍有数据被提取时,将表中的行设置为下面的行
$mmr1=$mmr;//第一个mmr计算值=mmr从提取
$diff=0;//差异定义为0
if($mmr2!=0){//如果mmr2有一个值(来自第二个循环)
$diff=$mmr1-$mmr2;
}
$mmr2=$mmr1;//这样两个值都可以出现在循环中
回声“$英雄
$result
$gamemode
$mmr
$diff”;
$i++;
$grapharray[$stmt['$hero']]=$stmt['$mmr'];
}
回声“;
$stmt->close();
}
$mysqli->close();
?>
这里是我重点关注的部分,我尝试使用while循环来填充一个HTML表中的数据,并使用它将数据单元(MMR和Hero name)放入一个数组中,以便以后创建图形时使用
以下是重点关注的部分:
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'];
}
while($stmt->fetch()){
//当仍有数据被提取时,将表中的行设置为下面的行
$mmr1=$mmr;//第一个mmr计算值=mmr从提取
$diff=0;//差异定义为0
if($mmr2!=0){//如果mmr2有一个值(来自第二个循环)
$diff=$mmr1-$mmr2;
}
$mmr2=$mmr1;//这样两个值都可以出现在循环中
回声“$英雄
$result
$gamemode
$mmr
$diff”;
$i++;
$grapharray[$stmt['$hero']]=$stmt['$mmr'];
}
我不确定如何使用此循环生成数组,因为我得到了以下错误:
致命错误:无法在第79行的C:\xampp\htdocs\Dota2HomePage\d2index.php中将mysqli\u stmt类型的对象用作数组
我想我做错了什么事 您应该使用绑定变量:
while($stmt -> fetch()){
...
$grapharray[$hero] = $mmr;
}
工作得很好。非常感谢。