gpt4 book ai didi

php - mysqli_fetch_array 数据放入数组并存储在 php 变量中

转载 作者:行者123 更新时间:2023-11-29 04:40:50 26 4
gpt4 key购买 nike

我正在处理这个 PHP 代码,我想检索每个成员在每次游泳划水时的 AVG 时间和单圈时间。我想先将数据存储到变量中,然后再检索到 HTML。

在下面的代码中我没有收到任何错误。仅获取笔画名称。不是 AVGLaps 或 LapsTime

$sql = "SELECT strokeType,AVG(totalLaps),AVG(totalSeconds) FROM performance where members_memberId = '$memberid' GROUP BY strokeType";

$result=mysqli_query($con,$sql);
$performanceData = array();

while($row = mysqli_fetch_array($result)) {
$performanceData[] =$row;
}

$stroke1 = $performanceData[0]['strokeType'];
$stroke2 = $performanceData[1]['strokeType'];
$stroke3 = $performanceData[2]['strokeType'];
$stroke4 = $performanceData[3]['strokeType'];

$strokeLaps1 = $performanceData[0]['totalLaps'];
$strokeLaps2 = $performanceData[1]['totalLaps'];
$strokeLaps3 = $performanceData[2]['totalLaps'];
$strokeLaps4 = $performanceData[3]['totalLaps'];

$strokeTime1 = $performanceData[0]['totalTime'];
$strokeTime2 = $performanceData[1]['totalTime'];
$strokeTime3 = $performanceData[2]['totalTime'];
$strokeTime4 = $performanceData[3]['totalTime'];

echo "StrokeName:".$stroke1.".";

最佳答案

你应该在你的 sql 查询中声明一个别名:

$sql = "SELECT strokeType,AVG(totalLaps) AS avgTotalLaps,AVG(totalSeconds) AS avgTotalSeconds FROM performance where members_memberId = '$memberid' GROUP BY strokeType";

然后像这样获取变量:

$strokeLaps1 = $performanceData[0]['avgTotalLaps'];
...

关于php - mysqli_fetch_array 数据放入数组并存储在 php 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29289104/

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