从stdClass访问值

时间:2015-06-28 01:16:25

标签: php codeigniter

请帮我这个.. 我将两列的值相加并减去...但我无法将其转换为整数..

$query = $this->db->query("
     SELECT SUM(`Bought_Cost`) - SUM(`Sold_Cost`) 
     FROM `Stock_txn` 
     WHERE `Stock_Id` = '$stock_Id'
");
$bought_sold_cost = $query->result();

如果我尝试var_dump($bought_sold_cost);

我明白了:

Array( [0]=> stdClass Object( [SUM(`Bought_Cost`) - SUM(`Sold_Cost`) ] => 3000.00 ) )

我想将数组用作变量......任何有想法的人?

1 个答案:

答案 0 :(得分:1)

为列命名:

$query = $this->db->query("SELECT SUM(Bought_Cost) - SUM(Sold_Cost) AS `difference` FROM Stock_txn WHERE Stock_Id = '$stock_Id'");
$bought_sold_cost = $query->result();

然后你会得到:

Array( [0]=> stdClass Object( [difference] => 3000.00 ) ) 

可以$bought_sold_cost[0]->difference

访问