-1

I'm trying to generate a pdf report but instead, I'm getting this error:

Type: Error

Message: Cannot use object of type mysqli as an array

MODEL

function pdf($post_id)
 {
 $result = $this->db->query("SELECT * FROM tb_data_utama WHERE nipBaru='$post_id'");
        return $result;
 }

CONTROLLER

 function getpdf()
    {
        $this->load->model('model_user');
        $this->load->library('pdf');
        $post_id = $this->uri->segment(3);
        $x['data'] = $this->model_user->pdf($post_id);
        $html = $this->load->view('GeneratePdfView', $x, [], true);
        $this->pdf->createPDF($html, 'mypdf', false);
    }
0

3 Answers 3

0

You need add ->result_array() to run your query !!!
From Docs

The above result() function returns an array of objects. Example: $row->title

$x['data'] = $this->model_user->pdf($post_id)->result_array(); <--- here
Sign up to request clarification or add additional context in comments.

Comments

0

You can pass simply return array by following

function pdf($post_id)
{
    $result = $this->db->query("SELECT * FROM tb_data_utama WHERE nipBaru='$post_id'");
    return $result->result_array();;
}

Comments

-1

By this $this->db->query() you are only executing the query. Not getting the result. Use one of the various methods to get the result - result() , row_array() , result_array() etc.

In your Model -

function pdf($post_id)
 {
  $result = $this->db->query("SELECT * FROM tb_data_utama WHERE nipBaru='$post_id'");
  return $result->row_array();
 }

In your View use values like that -

echo $data['nipBaru'];

2 Comments

This is a bad practice when you're going to get more than one row in result, it'll give you one record only
@MacRathod, I used it because the query will return data for only single post respective to the passed post_id

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.