这是我的代码。
$sql = "SELECT sum(col_1) as col_1_sum FROM table_1";
$run = mysqli_query($conn, $sql);
while($rows = mysqli_fetch_assoc($run)){
$col[1] = $rows['col_1_sum'];
}
我想知道什么是最有效的方法来获得9列的总和,并将它们存储在不同的变量中,比如$col2中第二行的总和等等。
发布于 2019-01-20 06:10:47
像这样更改您的sql
$sql = "SELECT sum(col_1) as col_1_sum ,sum(col_2) as col_2_sum, sum(col_3) as col_3_sum FROM table_1";
和获取
while($rows = mysqli_fetch_assoc($run)){
$col[1] = $rows['col_1_sum']; // in here its better to start this $col[] array's index from 0
$col[2] = $rows['col_2_sum'];
$col[3] = $rows['col_3_sum'];
}
或者像这个$col数组从第0个索引开始
while($rows = mysqli_fetch_assoc($run)){
$col[0] = $rows['col_1_sum']; // in here its better to start this $col[] array's index from 0
$col[1] = $rows['col_2_sum'];
$col[2] = $rows['col_3_sum'];
}
发布于 2019-01-20 06:22:34
我想使用二维数组来保持它为单变量
$variable[row_index][col_index] = $rows[col_index];
https://stackoverflow.com/questions/54273935
复制相似问题