-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);
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
milan
  • 17
  • 2

3 Answers3

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
daremachine
  • 2,678
  • 2
  • 23
  • 34
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();;
}
Mohit Rathod
  • 1,057
  • 1
  • 19
  • 33
-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'];
Alok Mali
  • 2,821
  • 2
  • 16
  • 32
  • 1
    This is a bad practice when you're going to get more than one row in result, it'll give you one record only – Mohit Rathod Jan 23 '20 at 13:02
  • @MacRathod, I used it because the query will return data for only single post respective to the passed post_id – Alok Mali Jan 23 '20 at 16:43