Skip to content
Advertisement

I’m facing this error Cannot use object of type mysqli as array

Controoler:

<?php

namespace AppControllers;
use CodeIgniterController;
use AppModelsServices;

class Detail extends Controller
{
    public function nadeem($id)
    {
       $model= new Services();
       $data = $model->select('text','title','image')->where('id',$id);
       $nadeem['result'] = $data->get();
       return view('service_detail', $nadeem);              
    }
}

view:

<!DOCTYPE html>
<html>
<head>
    <title>Service</title>
</head>
<body>



<?php if ($result): ?> 
    <?php foreach ($result as $key => $value): ?> 
         <p><?php echo $value['text'];?></p> 
    <?php endforeach; ?> 
<?php endif; ?>

</body>
</html>

I’m facing this error

Cannot use object of type mysqli as array

How can solve this problem.

Advertisement

Answer

Change the controller to this:

$model = new Services();
$nadeem['result'] = $model->select('text, title, image')->where('id',$id)->findAll();
return view('service_detail', $nadeem); 

Basically in your case you’re sending to the view the query object and not the query result.

This answer assumes that you’re using the codeigniter 4 Model and not a custom model since you didn’t post the model.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement