第一次使用php,我遇到了一些問題。 問題:在mysqli請求之後,我收到一個包含5列和多行的表的結果。我想用我剛剛收到的數據進行計算,並將我的calc的結果添加到我的數組中的NEW COLUMN中。 所以換句話說,我想向數組中添加一列,併爲每一行的結果填充此列。PHP - 將字段/列添加到數組
到目前爲止我的代碼如下所示,當然只打印,我從我的SQL查詢接收陣列:
<?php
error_reporting(0);
require 'init1.php';
if($result = $db->query("Select * from (select * from(SELECT * FROM `scores`
ORDER BY `battle_id` ASC,user_id asc,score desc) as t1 GROUP BY
t1.battle_id, t1.user_id) as t2 order by `battle_id` ASC,score desc")){
if($count = $result->num_rows){
echo '<p>' , $count, '<p>';
while ($row = $result->fetch_object()){
echo $row->battle_id, ' ' , $row->user_id, ' ' , $row->score, '<br>';
}
//instead of just printing the existing array, I would like to perform a
//calculation and add a result in a new column at the end of every single
//row
}
}
?>
非常感謝任何支持, 最好, 添
你說的是什麼計算? –
好吧,這有些複雜,但只是談論基本的數學。例如:新列中的任何新字段都應顯示結果(分數* 2 - 5)。 – gunjag
'echo $ row-> score * 2 - 5;'簡單,不是嗎? –