Home > front end >  why it's generating query without "from tablename" in codeigntier
why it's generating query without "from tablename" in codeigntier

Time:01-25

Here is my code.

public function get_records($event_id = null)
{
      $this->db->select('a.*');
      $this->db->from('attendee a');
      $this->db->join('event e','e.event_id = a.attendee_event_id','left');
      $this->db->join('users u','u.id = e.event_user_id','left');

      if($event_id != null)
        $this->db->where('a.attendee_event_id',$event_id);   

      if($this->ion_auth->is_agent())
      {
        $this->db->where('e.event_user_id', csession('user_id'));
      }
      else
      {
        $this->db->group_start();
          $this->db->where('e.event_user_id', csession('user_id'));
          $this->db->or_where('u.created_by', csession('user_id')); 
        $this->db->group_end();
      }

      $query = $this->db->get();

      return $query->result();
}

for this code i am getting below result

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ( `e`.`event_user_id` = '93' OR `u`.`created_by` = '93' ) AND `a`.`atte' at line 2

SELECT * WHERE ( `e`.`event_user_id` = '93' OR `u`.`created_by` = '93' ) AND `a`.`attendee_event_id` = '2'

Filename: models/Attendee_model.php

Line Number: 85

Whenever i am passing event_id, it's not working fine. but when I am not passing event_id, it's working fine.

I don't know what i am doing wrong here.

CodePudding user response:

CI is throwing that error because you are not providing the table name in the "get()" method.

Your code

$query = $this->db->get();

Correct approach

$query = $this->db->get('main_table_name');

For a better understanding of CI Query Builder read here

CodePudding user response:

$this->db->group_start();
 $this->db->where('e.event_user_id', csession('user_id'));
 $this->db->or_where('u.created_by', csession('user_id')); 
$this->db->group_end();

I have removed above code and it started working. btw it was unnecessary code. But I coudn't find the issue with this code though.

  • Related