Query that executes correctly in mysql client returns empty results in PHP / CodeIgniter

时间:2023-01-15 03:45:07

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

我很难找出为什么在mysql中工作的查询在CodeIgniter中不起作用。这是代码:

    $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中运行查询时,我得到:

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?

如您所见,CI正在构建的查询是有效的,它在mysql客户端中返回结果集。但是,查询在CodeIgniter中返回一个空数组。此页面上有许多其他查询正在处理。我不能为我的生活弄清楚为什么这个查询在CodeIgniter中不起作用?

2 个解决方案

#1


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.

在log_message条目之后放入die语句,再次运行代码,看看在手动运行查询时是否仍然看到结果。您看到的代码可能是正确的,而其他地方正在插入数据。

#2


0  

Change this line and try once:

更改此行并尝试一次:

$result = $q->result();

To:

$result = $q->result_array();

#1


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.

在log_message条目之后放入die语句,再次运行代码,看看在手动运行查询时是否仍然看到结果。您看到的代码可能是正确的,而其他地方正在插入数据。

#2


0  

Change this line and try once:

更改此行并尝试一次:

$result = $q->result();

To:

$result = $q->result_array();