PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 2, 2022

[FIXED] Mysql statement not working in CodeIgniter Model class

 March 02, 2022     codeigniter, mysql, php     No comments   

Issue

Currently I'm trying to retrieve all the entries from my data that are within a particular timeframe. To do this I have a method in my model class with the following statement:

public function get_records_all($st_date,$end_date){
        $sql = SELECT
            *
            FROM `crm_listings`  
            WHERE added_date BETWEEN '" . $st_date . "' AND '".$end_date."'
            ORDER BY `added_date` DESC;
            $response = $this->db->query($sql);
            echo $response;
     }

And in my controller class I'm using the following statement to show the output:

function fetch_status(){
            $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_all = $this->user_model->get_records_all($startDate,$endDate);
}

But this gives me the following error:

enter image description here


Solution

Try this

CodeIgniter gives you access to a Query Builder class. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases, only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

public function get_records_all($st_date,$end_date){  
 $this->db->where('added_date >=', $st_date);
 $this->db->where('added_date <=', $end_date);
 $this->db->order_by('added_date', 'DESC');
 return $this->get('crm_listings')->result();
}

For more use this link CI Query Builder CLass



Answered By - Danish Ali
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing