Codeigniter -从数据库中计数数组,

r8xiu3jd  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(48)

我有这个代码:

$product = $this->basic->get_data('products_imported',array('where'=>array('product_table'=>$product_table,'company_id' =>$company_id)));

字符串
正如你所看到的,结果是一个数组(),所以如果我想获取第一个结果,我必须使用这个:

$first_product = isset($product [0]['id']) ? $product [0]['id'] : 'empty';


我的问题是:我如何计算所有的结果?我必须像这样输出结果:
count all products = 10 //例如
first_product = 3 //产品ID
我尝试了num_rows,我有这个错误
第一个月
也试过这样做:

$this->db->select('id');
$this->db->from('products_imported');
$this->db->where(['product_table' => $product_table, 'company_id'=>$company_id]);
$products_counter = $this->db->count_all_results();


对于这段代码,我可以计算结果,但我不能像以前在第一段代码中那样获取第一个产品
你能帮我
感谢先进
尝试了许多方法,返回错误

f8rj6qna

f8rj6qna1#

你不能获取第一行,因为你没有获取任何东西。你正在使用$this->db->count_all_results(),它不返回一个对象。这也是为什么$this->db->num_rows();在计数为零后不返回任何结果的原因。
变化

$products_counter = $this->db->count_all_results();

字符串

$query = $this->db->get();
$product = $query->result_array();
$products_counter = count($product);


您还可以获得您正在寻找的$product[0]['id']

相关问题