dqst96444 2013-08-28 12:46
浏览 56
已采纳

如何使用php减去db值

How exactly do I subtract values from a database using php? I see plenty of examples using static variables such as

<?php
$first_number = 10;
$second_number = 2;
$third_number = 3;
$sum_total = $third_number + $second_number * $first_number;
print ($sum_total);
?>

However I'm looking to subtract one database value from another, then multiply that value by another db value. To give some more detail, I have an inventory database where I'm echoing the values into a table, I'm attempting to subtract the total quantity of an item from the minimum quantity, to see how many need to be ordered, then multiply the number of parts we need to order by the cost of that part. I've dug around and found a few possible methods such as

$query = "SELECT `db`, 
         (`minimumquantity` - `totalquantity`) AS `quantitytoorder`
        FROM `db` 
        WHERE id ='".$id."';"

and

<?php
$minimumquantity = $_GET['minimumquantity'];
$totalquantity = $_GET['totalquantity'];

$quantitytoorder = $minimumquantity - $totalquantity;
print ($quantitytoorder);
?>

Please before you laugh, I'm very much a beginner, can anyone point me in the right direction, or provide me with proper examples? My only real resource is the net and most examples I find are very high-level.

Field             Type        Null     Key   Default  Extra   
id                int(3)      NO       PRI   NULL     auto_increment 
partnumber        varchar(20) NO             NULL  
description       varchar(20) NO             NULL  
tonerprice        int(20)     NO             NULL  
totalquantity     int(20)     NO             NULL  
minimumquantity   int(20)     NO             NULL  
quantitytoorder   int(20)     NO             NULL  
replencost        int(20)     NO             NULL  
  • 写回答

3条回答 默认 最新

  • duanpanhuo0618 2013-08-28 13:00
    关注

    So assuming you know how to work with SQL in PHP, it's as simple as this:

    // STOP USING mysql, use mysqli or PDO, for demonstration purposes only
    $results = mysql_query('SELECT foo, bar, baz FROM table');
    
    while ($row = mysql_fetch_assoc($results)) {
        echo $row['foo'] - $row['bar'] * $row['baz'];
    }
    

    Assuming the columns are all numeric, that's all you need to do to subtract and multiply values from the database in PHP.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题