Php 在codeigniter中添加两个不同的查询

Php 在codeigniter中添加两个不同的查询,php,codeigniter,postgresql,Php,Codeigniter,Postgresql,我想添加两个查询,但它不起作用。这是我的 模型页: public function updateTable(){ $first= $this->db->query("select column1 from table where table_id='data1' AND field_id=6"); // Data type is numeric. When I query this, result is 0.0 $second= $this->db-&g

我想添加两个查询,但它不起作用。这是我的

模型页:

 public function updateTable(){

 $first=  $this->db->query("select column1 from table where table_id='data1' AND 
    field_id=6"); // Data type is numeric. When I query this, result is 0.0

 $second=  $this->db->query("select column1 from table where table_id='data2' AND 
    field_id=6"); // Data type is numeric also. When I query this, result is 0.0 also

 $total=$first+$second;

 }
当我尝试运行此程序时,收到一条错误消息

Message: Object of class CI_DB_postgre_result could not be converted to int
我怎样才能做到这一点或这是可能的

编辑:新信息

我想将这个$total用于if语句

 if ($total==0){
 //code here
 }
我该怎么办?

试试这个

 $total=$first->column1 + $second->column1;

如果您只需要总结果num,它将是

$first->num_rows() + $second->num_rows()
如果你想得到所有的结果,那么

array_merge ( $first->result (), $second->result );

此过程不正确,查询返回不添加的MYSql结果对象

是否要对(第1列)值求和

您可以在sql查询中添加列值,请尝试以下方法

public function updateTable(){

 $first=  $this->db->query("select column1 from table where table_id='data1' AND 
    field_id=6"); // Data type is numeric. When I query this, result is 0.0

 $second=  $this->db->query("select column1 from table where table_id='data2' AND 
    field_id=6"); // Data type is numeric also. When I query this, result is 0.0 also

 $total=$first->row()->column1 + $second->row()->column1;

 }


让我们看看CI用户指南中的第一个示例:

我们学到两件事:

  • $this->db->query(“您的查询”)
    不会返回结果,
    $query->result()
    会返回结果
  • 结果是一个对象数组。对象名称是列名
  • 如果您确定每个查询只返回一个结果。您可以按如下方式更改代码:

    $first=  $this->db->query("select column1 from table where table_id='data1' AND 
    field_id=6")->result()[0]->column1; 
    
    $second=  $this->db->query("select column1 from table where table_id='data2' AND 
    field_id=6")->result()[0]->column1;
    
    ====答案已修改如下=====

    分析是正确的,但上面的代码会抛出错误,因为php将result()[0]视为一个整体。

    你可以把它们分开

    $row = $this->db->query("select column1 from table where table_id='data1' AND 
    field_id=6")->result();
    $first = $row[0]->column1;
    
    或使用函数


    试试这个代码,我想这会管用的

    public function updateTable(){
    
     $first=  $this->db->query("select column1 from table where table_id='data1' AND 
        field_id=6")->row(); // Data type is numeric. When I query this, result is 0.0
    
     $second=  $this->db->query("select column1 from table where table_id='data2' AND 
        field_id=6")->row(); // Data type is numeric also. When I query this, result is 0.0 also
    
     $total=$first+$second;
    
     }
    

    什么是数据1?它是整数吗?@Vinod VT数字数据类型。如果其整数为1,则其数值为1.0。我想它们是一样的,不起作用。新的错误消息是消息:未定义属性:CI_DB_postgre_result:$column1…..尝试此$total=$first->row()->column1+$second->row()->column1;错误消息是message:Undefined property:CI_DB_postgre_result:$column1我尝试了上面的答案,但它说column1是未定义的变量。查询返回空结果,因此您得到了
    column1
    variable,@Kino“从表中选择column1,其中表id='data1'和字段id=6”运行查询并确保返回结果?只需在最后一个查询中添加行()
    $row = $this->db->query("select column1 from table where table_id='data1' AND 
    field_id=6")->result();
    $first = $row[0]->column1;
    
      $first=  current($this->db->query("select column1 from table where table_id='data1' AND 
    field_id=6")->result())->column1; 
    
    public function updateTable(){
    
     $first=  $this->db->query("select column1 from table where table_id='data1' AND 
        field_id=6")->row(); // Data type is numeric. When I query this, result is 0.0
    
     $second=  $this->db->query("select column1 from table where table_id='data2' AND 
        field_id=6")->row(); // Data type is numeric also. When I query this, result is 0.0 also
    
     $total=$first+$second;
    
     }