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
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
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();;
}
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'];
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);
}
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