0

Hello I got the this result

A PHP Error was encountered

Severity: Error

Message: Cannot use object of type CI_DB_mysqli_result as array

Filename: controllers/Threads.php

Line Number: 48

Backtrace:

This is my codes in Controller

// category
public function threadsview($slug) {
    // get threads category
    $data['item'] = $this->threads_model->getThreadsCategory($slug);

    $data['title'] = $data['item']['catname'];
    $data['desc'] = '';

    $this->load->view('themes/default/header', $data);
    $this->load->view('threads/threadsview', $data);
    $this->load->view('themes/default/footer');
}

And the codes in my Model

 //get threads categories by slug
    public function getThreadsCategory($slug) {
        $this->db->order_by('id','asc');
        $this->db->where('slug',$slug);
        $query = $this->db->get('threads_cat');
        return $query;
    }

The error is from this code

$data['title'] = $data['item']['catname'];

I hope someone can help me regarding on this..

1
  • $data['item'] is not an array. check if it's an array. Commented Dec 10, 2017 at 23:44

4 Answers 4

5

What you need to do is get a result or row, and then check if there was anything in it. So when you do your query:

$query = $this->db->get('threads_cat');

Before you return anything you should do this:

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

return FALSE;

Or if you are just expecting a single row:

if( $query->num_rows() == 1 )
{
    return $query->row_array();
}

return FALSE;

Be sure to check if $data['item'] == FALSE before trying to use it:

if( $data['item'] !== FALSE && is_array( $data['item'] ) )
{
    // $data['item'] is an array and can be used as an array
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you dude I`ve already figure it out and its working now
Glad to hear it. Be sure to give some credits if credits are due.
Yeah sure.. by the way I appreciate your help.
So many people get tripped up over this. I would add, generally instead of checking if it is an array, check if it has any rows! count($result) > 0
1

Thank you for your response I`ve made it and its working now

public function threadsview($slug) {
        // get threads category
        $data['item'] = $this->threads_model->getThreadsCategory($slug);
        if($data['item']->num_rows()<=0){
            $data['title'] = 'Opps!, 404 page not found';
            $this->load->view('themes/default/header', $data);
            $this->load->view('errors/html/error_404');
            $this->load->view('themes/default/footer');
        }else
        foreach ($data['item']->result() as $res) {


        $data['title'] = $res->catname;
        $data['desc'] = $res->description;

        $this->load->view('themes/default/header', $data);
        $this->load->view('threads/threadsview', $data);
        $this->load->view('themes/default/footer');
        }
    }

Comments

1

Make sure your returning an array or an object in your modal

$data['item'] = $this->threads_model->getThreadsCategory($slug);

in your model if your using

$this->db->from($table); $query = $this->db->get(); return $query->row(); // this is returning the first row as object & not array

In controller

$data['title'] = $data['item']->catname;

for array you could use something like this

$query->first_row('array'); //this is returning the first row as Array

In controller

$data['title'] = $data['item']['catname'];

//if your still having a problem you could print your data in controller just comment your load->view() and add this line

print_r("<pre>"); print_r($data['item']); print_r("</pre>");

Comments

0

$data['item'] is not an array, probably because you didn't actually fetch the results into an array.

Inside your function, fetch first the result of the query into an array.

public function getThreadsCategory($slug) {
    $this->db->order_by('id','asc');
    $this->db->where('slug',$slug);
    $query = $this->db->get('threads_cat');
    $result = $query->result_array();
    return $result;
}

1 Comment

Thanks dude but this code still not working the error is the same. And I`ve already figure out how to fixed this by the way thank you very much for your response I appreciate it.

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.