dongya4089 2016-03-26 02:28
浏览 54
已采纳

mysql:将数学运算存储为新数据库

I have table, from this table, I want to do some math operation (such as sum) then I want to save the result in a separate column from the table.

so this is my table

table: xxx

!  code   !   A  !   B  !   C  !
---------------------------------
!  0001   !   4  !   2  !      !
!  0002   !   2  !   1  !      !
!  0003   !   1  !   4  !      !
!  0004   !   3  !   5  !      !

then I use this code to call the query, and save the math operation.

$mySql = "SELECT *  FROM xxx ORDER BY code ASC "; 
$myQry = mysql_query($mySql, $koneksidb)  or die ("Query salah : ".mysql_error());
while ($myData = mysql_fetch_array($myQry))

$sum = $myData['A'] +  $myData['A']

$mySql  = "UPDATE xxx  SET C='$sum' WHERE code='$Kode'";
$myQry  = mysql_query($mySql, $koneksidb) or die ("Gagal query".mysql_error());
while ($myData = mysql_fetch_array($myQry))
{
    $Kode = $myData['code'];
}

my problem is, it's not come up like what I want. I want like this:

table: xxx

!  code   !   A  !   B  !    C  !
---------------------------------
!  0001   !   4  !   2  !    6  !
!  0002   !   2  !   1  !    3  !
!  0003   !   1  !   4  !    5  !
!  0004   !   3  !   5  !    8  !

but this is what I've got

table: xxx

!  code   !   A  !   B  !    C  !
---------------------------------
!  0001   !   4  !   2  !    6  !
!  0002   !   2  !   1  !    6  !
!  0003   !   1  !   4  !    6  !
!  0004   !   3  !   5  !    6  !

any help? I think, my code just take data from the first code, how can I fix this?

  • 写回答

3条回答 默认 最新

  • dongqiao3833 2016-03-26 02:40
    关注

    There are several issues here and this task can be done much easier. However since this sounds like you are trying things and want to learn let's concentrate on you code.

    The immediate problem is the structure of your code. You perform your math operation, however your structure does not store the result as you expect it to. You have to change the structure to something like that:

    $selectQuery = "SELECT * FROM xxx ORDER BY code ASC"; 
    $selectResult = mysql_query($selectQuery, $koneksidb) or die ("Query salah: ".mysql_error());
    while ($myData = mysql_fetch_array($selectResult)) {
        $sum = $myData['A'] +  $myData['B'];
        $code = $myData['code'];
        $updateQuery = "UPDATE xxx SET C='$sum' WHERE code='$code'";
        mysql_query($updateQuery, $koneksidb) or die ("Gagal query: ".mysql_error());
    }
    

    Especially not the curly brackets used with the while command: while (...) { ... }.

    A general warning: you are using the old, outdated and deprecated mysql extension here. You should take a look at the current alternatives mysqli or PDO and learn about the advantages of "prepared statements" and "parameter binding" for your code, so that you do not run into the typical issue of "sql injection vulnerability".

    As mentioned in the beginning it certainly is possible to simplify this whole task by not performing the math computation in php, but directly in mysql. That way you would only need a single sql update statement which does all the job: UPDATE xxx SET C=A+B;. But as mentioned before your question looks like you are trying to work into this field, so it probably is a very good idea to first get your own code to work before moving on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件