【问题标题】:Mysql query order by desc limit in codeigniterMysql查询顺序按codeigniter中的desc限制
【发布时间】:2019-07-18 11:01:45
【问题描述】:

我有一个如下表目标

id |    subject        |      achievement                |     staff_id
1     Target January          1150000                            1
2     Target January          1350000                            2
3     Target February         20000000                           1
4     Target February         23500000                           2
5     Target January          1500000                            3

我想展示的是在 codeigniter 中使用 sql 查询,如

SELECT * FROM `target` WHERE `staff_id`='$id' ORDER BY 'id' DESC LIMIT 3,1

我已经尝试在 codeigniter 中使用 get where、order by 和 limit 查询,但是屏幕变黑了

这是模型代码

    $id=get_staff_user_id();
    $this->db->get_where('target', array('staff_id'=>$id,));
    $this->db->order_by('id','desc');
    $this->db->limit(3, 1);  
    $query=$this->db->get();
    return $query;

这是控制器代码

 $data['target'] = $this->target_model->getAllTarget()->result();

这是查看代码

 <?php foreach($targets as $target){ 
  if($total >= floatval($target->achievement)) {
                $percent = 100;
            } else {
                if ($total !== 0) {
                    $percent = number_format(($total * 100) / $target->achievement, 2);
                }
            }
  echo $percent;
 ?>

我的代码中的错误在哪里?

谢谢

【问题讨论】:

  • 去掉$id后面多余的逗号$this-&gt;db-&gt;get_where('target', array('staff_id'=&gt;$id));,开启报错功能就可以知道是什么错误了。
  • " 但是屏幕变黑了" 你的意思是 PHP 的白屏死机检查错误日志是否有错误。
  • "去掉多余的逗号 $this->db->get_where('target', array('staff_id'=>$id)); $id" 不会帮助@DevsiOdedra,因为像topicstarter那样使用逗号实际上是valid代码并且不会给出解析错误..
  • 你只是建议,知道确切的错误启用错误报告。
  • foreach($target as $target 将目标替换为目标,因为您将目标从控制器传递到视图。

标签: php mysql codeigniter


【解决方案1】:

问题是 - get_where() 已经返回一个数据库结果实例 - 为了解决您的问题,您必须执行类似的操作

return $this->db
    ->select('*')
    ->from('target')
    ->where('staff_id', get_staff_user_id())
    ->order_by('id', 'desc')
    ->limit(3,1)
    ->get();

【讨论】:

  • @Vickel - ofc 这是问题get_where() 的行为类似于get() 所以在他的情况下-他基本上执行2个查询-一个仅以员工ID作为条件-第二个以order by和限制但导致错误 - 因为没有来自表(如果选项 db_debug 是 false - 该网站显示为空白)
  • 糟糕,我没有看到第二个 get(),抱歉!
【解决方案2】:

在模型中:

去掉多余的逗号

    $id=get_staff_user_id();
    $this->db->get_where('target', array('staff_id'=>$id));
    $this->db->order_by('id','desc');
    $this->db->limit(3, 1);  
    $query=$this->db->get();
    return $query;

在控制器中:

将目标更改为从相同数组名称获取的 foreach 中的目标;

$data['targets'] = $this->target_model->getAllTarget()->result();

【讨论】:

  • array('staff_id'=&gt;$id,) 是valid PHP 代码,不会给出解析器错误..
  • 是的,它是有效的,但为了安全起见,我刚刚提到了 controller 方面的主要问题,因为名称与 view 不同。
  • “是的,它是有效的,但为了安全起见” 使用array('staff_id'=&gt;$id,) 是安全的,因为它会not 生成一个“幽灵”元素,您可能会期望它当你阅读代码array('staff_id'=&gt;$id,)时在这里。但我同意使用array('staff_id'=&gt;$id,)看起来有点奇怪..
  • 是的@RaymondNijland,它看起来很奇怪,感谢分享:)
  • 没关系,让我奇怪的是如果代码只有一行,比如 return $this->db->get_where('target', array('staff_id'=>$id));它工作正常
猜你喜欢
  • 1970-01-01
  • 2017-03-29
  • 2016-05-09
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多