doudi1750 2013-08-26 17:03
浏览 38
已采纳

MySQL使用CodeIgniter查询问题

So I was able to find the root cause of my earlier problem.

Turns out it was the querying that is the problem. Well, not on my side, at least not that I think so. This problem has been bugging me for many hours now. Unfortunately, it something I don't know how to fix.

Please check this code:

public function get_calendar_content($y,$m){

   $query = $this->db->query("SELECT * from events WHERE event_date LIKE '$y-$m%'");
   $content=array();
   foreach($query->result() as $q){
       $content[substr($q->event_date,8,2)]=$q->event_details;
   }
   return $content;

}

This function ignores whatever I supply for the $m, or the month. It only cares about the year. Plus, when I tried selecting all data from the events table instead of having a where clause, it still only returns the entries or data from the events table which is is dated in the month of August.

When I tried writing the where clause explicitly to event_date LIKE '2013-09%', it wouldn't return any data at all. I don't understand what's going on. For some unknown reason, the function would only return data from August. I already tried writing the query on phpMyAdmin, and it works just how it's supposed to do. It only produces irregularity in my app. What could my problem be?

Edit: I'm now using multidimensional array. It fixed the confusion about multiple events, but still does not return data from other months.

public function get_calendar_content($y,$m){
$query = $this->db->query("SELECT * from events WHERE event_date LIKE '$y-$m%'");
           $content=array();

       foreach($query->result() as $q){
           $content[substr($q->event_date,8,2)][]=$q->event_details;
       }
       return $content;
}
  • 写回答

1条回答 默认 最新

  • duanguangsong2380 2013-08-26 17:10
    关注

    The problem is in how you're building the return array:

    substr($q->event_date,8,2) will get the day for each event (1-31) and you're using it as your key, but an array can't have repeating keys, so only the last value will be kept.

    The solution is to change the key values for your array so they don't repeat

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助