3

I'm using the latest CodeIgniter version (3.1.4). I'm running the following basic query:

$query = $this->db->query("SELECT * FROM mytable;");

and getting a blank result - num_rows and row_data are both blank. Here's a print_r output of $query:

CI_DB_mysqli_result Object
(
    [conn_id] => mysqli Object
        (
            [affected_rows] => 2
            [client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: b5c5906d452ec590732a93b051f3827e02749b83 $
            [client_version] => 50012
            [connect_errno] => 0
            [connect_error] => 
            [errno] => 0
            [error] => 
            [error_list] => Array
            (
            )

            [field_count] => 7
            [host_info] => Localhost via UNIX socket
            [info] => 
            [insert_id] => 0
            [server_info] => 5.6.35
            [server_version] => 50635
            [stat] => Uptime: 2988249  Threads: 2  Questions: 13445030  Slow queries: 9  Opens: 784  Flush tables: 1  Open tables: 469  Queries per second avg: 4.499
            [sqlstate] => 00000
            [protocol_version] => 10
            [thread_id] => 922470
            [warning_count] => 0
        )

    [result_id] => mysqli_result Object
    (
        [current_field] => 0
        [field_count] => 7
        [lengths] => 
        [num_rows] => 2
        [type] => 0
    )

    [result_array] => Array
    (
    )

    [result_object] => Array
    (
    )

    [custom_result_object] => Array
    (
    )

    [current_row] => 0
    [num_rows] => 
    [row_data] => 
)

Can anyone help me figure out what's wrong here?

2 Answers 2

3

Those values will remain blank until you call one of the methods that generate a result - documented here.

Try this

$query = $this->db->query("SELECT * FROM mytable;");
echo "Rows: ". $query->num_rows(). "<br>";
$data = $query->result();
var_dump($data);

See anything?

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

1 Comment

Yes, works now, thank you. I take it this is because of the move from mysql to mysqli in more recent versions of CI? Was using version 2.x prior to this, and never had issues.
0

I hope this change will helps you

$query = $this->db->query("SELECT * FROM mytable;");
$data = $query->result();
return $data;

you can rewrite this query in other ways

$this->db->select('*');
$this->db->from('mytable');
$data=$this->db->get();

for data result

return $data->result();

for number of rows

 return $data->num_rows();

Comments

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.