1

I am stuck trying to figure out why a query that works in mysql does not work in CodeIgniter. Here's the code:

    $this->db->select('*');
    $this->db->from('table');
    $this->db->where('table.project_id', $project_id);
    $this->db->where('table.user_id', $user_id);
    $q = $this->db->get();
    $result = $q->result();
    log_message('error', 'error message='.$this->db->_error_message());
    log_message('error', 'error number='.$this->db->_error_number());
    log_message('error', 'result='.print_r($result, true));
    log_message('error', 'last query='.$this->db->last_query());

The log output on this looks like this:

ERROR - 2013-09-20 08:53:03 --> error message=
ERROR - 2013-09-20 08:53:03 --> error number=0
ERROR - 2013-09-20 08:53:03 --> result=Array
(
)

ERROR - 2013-09-20 08:53:03 --> last query=SELECT `table`.*
FROM (`table`)
WHERE `table`.`project_id` =  '99'
AND `table`.`user_id` =  '1927'

When I run the query in mysql I get:

mysql> SELECT `table`.*
    -> FROM (`table`)
    -> WHERE `table`.`project_id` =  '99'
    -> AND `table`.`user_id` =  '1927';
+------+---------+----------+------------+----------------+
| id   | user_id | group_id | project_id | accepted_terms |
+------+---------+----------+------------+----------------+
| 2328 |    1927 |        8 |         99 |              0 |
+------+---------+----------+------------+----------------+
1 row in set (0.00 sec)

As you can see, the query that CI is constructing is valid, and it returns a result set in the mysql client. However, the query returns an empty Array in CodeIgniter. There are many, many other queries that are working on this page. I can't for the life of me figure out why this query isn't working in CodeIgniter?

1
  • 1
    Try it without active record, just for the sake of debugging. $q = $this->db->query('SELECT * FROM table WHERE project_id = 99 AND user_id = 1927'); Also, try to print_r($result);die(); before you do any logging and see if you still get no result. Commented Sep 20, 2013 at 16:34

2 Answers 2

1

Put in a die statement after your log_message entries, run the code again and see if you still see the result when you manually run the query. It is possible that the code you see is correct and somewhere else is inserting the data.

Sign up to request clarification or add additional context in comments.

1 Comment

tom: that was it...the record got added after the query!
0

Change this line and try once:

$result = $q->result();

To:

$result = $q->result_array();

1 Comment

Sorry, no luck...exact same output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.