【问题标题】:Unable to retrieve status count after passing Mysql query通过 Mysql 查询后无法检索状态计数
【发布时间】:2021-11-26 06:56:05
【问题描述】:

目前我正在使用 CodeIgniter 在特定时间范围内检索我的数据。所有这些条目都有一个状态。我正在尝试计算特定时间范围内存在的所有数据。目前这是我的模型类,其中我有以下条目来返回特定日期范围内的所有条目:

public function get_records($st_date,$end_date){
        $this->db->select('*');
        $this->db->from('crm_listings');
        $this->db->where('cast(added_date as date) BETWEEN "' . $st_date . '" AND "' . $end_date . '" AND status= "D"');
        
        return $this->db->count_all_results();
     }

还有我的控制器类:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
   class Testcontroller extends CI_Controller
   {  
      public function index() 
      { 
      $this->load->view('crm/test');      
       }

function fetch_status(){
            $output ='';
            $startDate = '';
            $endDate = '';
            $this->load->model('crm/user_model');
  
            if($this->input->post('startDate')){
              $startDate = $this->input->post('startDate');
            }
            if($this->input->post('endDate')){
               $endDate = $this->input->post('endDate');
             }
            $data = $this->user_model->get_records($startDate,$endDate);
            
            $output .= '
              <div class="table-responsive">
                 <table class="table table-bordered table-striped">
                    <tr>
                    <th>Draft</th>
                    </tr>
              ';
           if($data->num_rows() > 0)
           {
            $output .= '
               <tr>
                  <td>'.$data.'</td>
               </tr>
            ';
            }
           else
           {
              $output .= '<tr>
                 <td colspan="7">No Data Found</td>
                 </tr>';
           }
           $output .= '</table>';
           echo $output;
           }
}     
?>

所以到目前为止,当我在 $data 之后评论我的控制器类中的所有内容并只发表声明 echo $data; 时,我得到了我想要的输出。但是当我将它包裹在 &lt;td&gt;'.$data.'&lt;/td&gt; 周围时,它不会给出输出。

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    echo $this->db->count_all_results();

    行编辑你的函数
    function get_records($st_date,$end_date){
    echo $this->db->count_all_results();// change this code
    // to
    return $this->db->get();
    }
    

    为确保有任何数据,请在调用该方法/函数后尝试print_r($data)

    $data = $this->user_model->get_records($startDate,$endDate);
    print_r($data) // check data
    

    【讨论】:

    • 我这样做了,它给了我一个 CI_DB_mysqli_result 类的对象无法转换为字符串错误。并且使用 print_r 我得到了以下 CI_DB_mysqli_result 对象( [conn_id] => mysqli 对象( [affected_rows] => 48 ...
    • 对于错误尝试this解决方案
    • @JayVijayModi,所以你有结果,然后将你的对象 $data 转换为 $data-&gt;result()
    • @shevanto,所以我必须这样做 '.$data->result().' ?
    • @JayVijayModi,如果你想得到 count ,就做$data-&gt;num_rows()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    相关资源
    最近更新 更多