【问题标题】:Faster way to fetch a single data from db with codeigniter [duplicate]使用codeigniter从数据库中获取单个数据的更快方法[重复]
【发布时间】:2020-05-29 06:56:43
【问题描述】:

嗨,我只想从我的数据库的一个结果中获取一列。所以我就这样做了

$this->db->select('myclolumn')->where('id',$id);
$query = $this->db->get('mytable');
$temp=$query->row_array();
$finalresult=$temp['mycolumn'];

也许有更简单或更快的方法来做同样的事情?

【问题讨论】:

  • 解释实际问题是什么。
  • 没问题,我问自己是否有更快的方法将“mycolumn”放入 $finalresult 中,例如 $temp=$query->result->mycolumn
  • 使用row() 而不是row_array()。然后你可以使用$finalresult->columnName
  • @DanishAli 这就是我要找的 - 非常感谢。
  • 这是一个研究不足的重复问题的可能性有多大?提问前请先搜索。

标签: php codeigniter


【解决方案1】:

所以基于丹麦阿里的评论

$this->db->select('myclolumn')->where('id',$id);
$query = $this->db->get('mytable');
$finalresult=$query->row()->mycloumn;

【讨论】:

    【解决方案2】:

    你可以这样做:

    $finalresult = $this->db->select('myclolumn')
                            ->where('id',$id)
                            ->get('mytable')->row()
                            ->mycolumn;
    

    全部在一个命令中,而不是大量的变量和额外的代码。

    如果您需要在该行为空时提供默认值,请将命令中断为:

    $row = $this->db->select('myclolumn')
                            ->where('id',$id)
                            ->get('mytable')
                            ->row();
    $finalresult = ($row)?$row->mycolumn:'your default value';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多