【问题标题】:Codeigniter 2.2.6 find min value from sql columnCodeigniter 2.2.6 从 sql 列中找到最小值
【发布时间】:2017-05-03 18:26:07
【问题描述】:

我有一个错误:

Array to string conversion

在我的模型中,我有这个:

public function get_min(){      
    $this->db->select_min('sq_place');
    $query = $this->db->get('places');
    return $query->result();    
}

在我的转储数据中,我得到了这个:

array (size=1)
  0 => 
    object(stdClass)[32]
      public 'sq_place' => string '110' (length=3)

我使用 CodeIgniter v 2.2.6。如何将其转换为 110 之类的字符串而不会出错?

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    使用

    $result = $query->result_array() ; // to get response as array.
    

    那么。访问您的字符串值

    $val = $result[0]['sq_place'];
    echo $val; //This should print your string val and wont give mentioned error
    

    【讨论】:

    • 在我的控制器中我有: $this->data['sqv'] = $this->place_m->get_min();在我看来 同样的结果。
    • 在你看来应该是,$sqv1 = $sqv[0]; echo $sqv1->sq_place;
    • 没有相同的结果。我知道我已经很接近了,但出了点问题。
    • 你能分享一下你视图中的var_dump($sqv)吗?
    • 在我看来解决方案 $sqv[0]['sq_place']
    【解决方案2】:

    好的,据我了解,您需要将最小值作为字符串返回(返回参数)。所以这是最好的方法:

    public function get_min(){      
       $this->db->select_min('sq_place');
       $query = $this->db->get('places');
       $min = 0;
       if($query->num_rows > 0){
          // It returns single row as an object from database
          $min = $query->row()->sq_place;
       }
       return $min ;    
    }
    

    当您打印 $min 值时,您将获得单个值本身。如果您需要其他方式的结果,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2016-05-22
      • 2021-06-23
      • 2020-12-02
      • 2017-05-30
      • 2011-01-17
      • 2011-04-27
      相关资源
      最近更新 更多