简体   繁体   中英

mysqli_fetch_array data into array and store in php variables

i'm working on this PHP Code that i want to retrieve AVG Time and LAP time for each member in each swimming stroke. i want to store the data first into variables before retrieve to the HTML.

in following code i'm not getting any error. Only Getting Stroke Name. not AVGLaps or 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.".";

You should declare an alias in your sql query:

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

And then get the vars like:

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

如果要使用totalLaps和totalTime作为关联索引,则需要对SQL语句中的列进行别名化。

SELECT strokeType, AVG(totalLaps) totalLaps, AVG(totalSeconds) totalTime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM