Matthew Sciamanna
Matthew Sciamanna

Reputation: 41

Codeigniter: Select based inquiries

I am having a strange issue with codeigniter where no SELECT based inquiries seem to be going through to my sql database. Originally I had something like:

    public function __construct(){
        $this->load->database();
    }
    public function foo(){
         $this->db->query("SELECT * FROM 'table_name'");
         }

and this returned an empty result array when inspected with print_r. So then I switched to:

    public function __construct(){
        $this->load->database();
    }
    public function foo(){
         $query = $this->db->get('tracker');
         print_r($this->db->last_query());
         print_r($query);
         }

$this->db->last_query showed me that even though a correctly formatted statement, SELECT * FROM 'table_name', was being attempted, nothing was showing up in the results array. Troubleshooting has shown me that:

1.insert statements like "$this->db->query("INSERT INTO table VALUES"...) still work fine.

  1. The SQL queries being sent with get() still work on the SQL tab of phpmyadmin

  2. The sql statements appear to be getting through to the database because I am returned a mysqli conn_id.

  3. Changing browsers and/or restarting mamp doesnt work.

If anyone has had this issue before or sees something obvious that I'm missing I would be very grateful to know.

Thank you in advance!

Matt

Upvotes: 0

Views: 81

Answers (3)

JYoThI
JYoThI

Reputation: 12085

For your both method you need to get the record set like below

Method 1 : use result_array to get the result as array .

    public function foo(){
         $q= $this->db->query("SELECT * FROM 'table_name'");
         $result= $q->result_array();
         print_r($result);
    }

Note : you can also use this $result= $q->result(); you will get record set as objects.

Method 2 :

      public function foo(){
         $query = $this->db->get('tracker');
         $result = $query->result_array();

         print_r($result );
         //print_r($this->db->last_query());

      }

Upvotes: 1

Shihas
Shihas

Reputation: 814

Remove using the table name in single quotes.

public function foo()
{
     $query = $this->db->query("SELECT * FROM table_name");
     $result = $query->result_array();
     print_r($result);
}  

And also in order to see the compiled query use:

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

Upvotes: 1

Nere
Nere

Reputation: 4097

To display the result, you need to get ->result(). For example:

$obj_table = $this->db->query("SELECT * FROM 'table_name'");
print_r($obj_table->result());

You may refer this documentation for further information: https://www.codeigniter.com/userguide3/database/results.html

Hopes answered your question.

Upvotes: 1

Related Questions