CodeIgniter: DB->query() returns 0 rows

I have a relatively complex SELECT statement that I run directly via $this->db->query($sql);. For some reason this returns no rows. The way I’m checking the result is the advised way per the documentation:

        if($query->num_rows() > 0)
        {
            return $query->result();
        }

I’ve run the same query directly through mysqli using the following code, and it works and returns results as expected:

<?php

$db = new mysqli('localhost', 'username', 'password', 'database');

$sql = "<statement goes here>";

$result = $db->query($sql);

echo var_dump($result);
echo var_dump($result->fetch_all());

while ( $val = $result->fetch_object() )
{
    echo var_dump($val);
}

$result->close();
$db->close();

?>

Obviously the query also runs fine directly in mysql.

This is what I’ve done to debug the issue thus far:

  1. Logged what $sql is before running the query, it is running the query that I expect
  2. Logged what $this->db->last_query() is after the $this->db->query() call, and it confirms that it ran the query completely unchanged.
  3. Turned the profiler on for the controller, confirmed that the query is there and it is the same query that I expect.
  4. Added "EXPLAIN " to the beginning of the SQL statement and logged the result, confirming that the correct explain is coming back.

I’ve also confirmed that 1, 2, and 3 are all showing the same SQL query.

I’m running 3.0.1 release, but am experiencing the same issue on 3.0.2-dev.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 38 (16 by maintainers)

Most upvoted comments

look https://github.com/bcit-ci/CodeIgniter/issues/3812 use $result->num_rows() not $result->num_rows