dongzhan5246 2016-10-05 08:38
浏览 77

在获取数据库中的值后,在codeigniter 3中获取未定义的索引返回日期?

I am fetching values from database and the values are successfully shown when i use print_r but i get an error when i try to echo the values out it gives me an error of Message:

Undefined index: return_date

my code for controller is as follows

public function latebooks(){
    $now = date('m/d/Y');
    foreach($table as $q){}
    $table = $q->department_name;
    $table = strtolower($table);

and my code for my model is as follows:

public function timecalculations($table){
    $query= $this->db->get_where('issue_books',array('department_id'=>$table));
    return $query->result_array();

and the code in the view is as follows


 * Created by PhpStorm.
 * User: workspace
 * Date: 05-10-2016
 * Time: 13:46
include 'header.php';
<br /><br />
echo $query['return_date'];
include 'footer.php';

please forgive as i am novice here!Thanks in advance

  • 写回答

4条回答 默认 最新

  • du0173 2016-10-05 08:43

    You are using result() function in your MODEL for getting records, this will return data into object form not in array.


    echo $query['return_date'];

    Should be:

    echo $query->return_date;

    After your modification in your question, you must need to check what are you getting in print_r($run).

    If you have data in multi-dimensional array than you can use loop here, this will print all data either having one record or multiple.

    foreach ($query as $key => $value) {
        echo $value->return_date;

    If you want to use result_array() function than you can use like:

    foreach ($query as $key => $value) {
        echo $value['return_date'];

    This post will help you to understand both functions: codeigniter, result() vs. result_array()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dsnm64969 2016-10-05 08:42

    Change this return $query->result(); in your model as follows

    return $query->result_array();

    ** return $query->result(); this returns as object. If want to use this then access data like echo $query->return_date;

  • duancashi1362 2016-10-05 08:44

    $query->result() returns an array of objects, because it doesn't know whether there's supposed to be 1 result or 100,000 results. To just fetch a single result, you'd use $query->row() instead.

    But that would still give you the result as an object instead of an array, so if you want to use $query['return_date'] in your view, you'd use $query->row_array() to get the row as an array.

  • dongzhang1875 2016-10-05 08:49

    In your controller make these following changes.


    In your view page

    echo $data[0]['return_date'];



  • ¥15 Tpad api账户 api口令
  • ¥30 ppt进度条制作,vba语言
  • ¥15 stc12c5a60s2单片机测光敏ADC
  • ¥15 生信simpleaffy包下载
  • ¥15 请教一下simulink中S函数相关问题
  • ¥15 在二层网络中,掩码存在包含关系即可通信
  • ¥15 端口转发器解析失败不知道电脑设置了啥
  • ¥15 Latex算法流程图行号自定义
  • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
  • ¥40 基于51单片机实现球赛计分器功能