1

我有一段用 Codeigniter 框架编写的 PHP 代码,它什么都不返回(一个空集)。看看它,告诉我它有什么问题。

function sbsn($serial){
    $this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
    $this->db->from('asset_types,asset_brands,assets');
    $this->db->where('assets.type_code','asset_types.code');
    $this->db->where('assets.brand_code','asset_brands.code');
    $this->db->where('serial_no',$serial); 
    $result = $this->db->get();
    return $result;
}
4

3 回答 3

3

嗨,db get 你只得到结果集而不是记录从结果中得到结果你必须做以下事情

$this->db->get()->result(); // will return an array of objects
$this->db->get()->result_array(); //will return result in pure array
$this->db->get()->row() // just single row as object
$this->db->get()->row_array() // just single row as array

you can use $result to perform the same above things
$result->result();
$result->row();

有关更多信息,请阅读生成结果用户指南

于 2013-02-17T10:10:39.057 回答
1

改变:

$result = $this->db->get();

$result = $this->db->get()->row_array();  //to get single row
//OR
$result = $this->db->get()->result_array();  //to get multiple rows

或尝试使用 JOIN,例如

$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('assets');
$this->db->join('asset_types', 'asset_types.code = assets.type_code', 'left');
$this->db->join('asset_brands', 'asset_brands.code = assets.brand_code', 'left');
$this->db->where('assets.serial_no',$serial); 
$result = $this->db->get()->result_array();
return $result;
于 2013-02-17T10:09:32.007 回答
1

通过以下方式连接表可以轻松解决该问题。

$this->db->select('at.name as type_name,ab.name as brand_name');
$this->db->from('asset_types as at,asset_brands as ab');
$this->db->join('assets as a', 'a.type_code = at.code and a.brand_code as ab.code');
$this->db->where('a.serial_no',$serial); 
$result = $this->db->get()->result_array();
return $result;
于 2013-02-18T07:03:47.340 回答