CodeIgniter Forums
class CI_DB_mysqli_result could not be converted to string - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: class CI_DB_mysqli_result could not be converted to string (/thread-65774.html)



class CI_DB_mysqli_result could not be converted to string - davy_yg - 07-22-2016

Hello,

I am trying to fix this error message but could not find a way out yet.  Can anyone help me out?  Thanks in advance.


A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysqli_result could not be converted to string
Filename: views/pcategories.php
Line Number: 53
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\EcommerceGiondaCI\application\views\pcategories.php
Line: 53
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\EcommerceGiondaCI\application\controllers\Cpages.php
Line: 215
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\EcommerceGiondaCI\index.php
Line: 315
Function: require_once



models/Pages_model.php


PHP Code:
public function call_menu()
        {
                
$this->db->select('*')->from('pages');
        
             
   $query $this->db->get();

                return 
$query->result();        
        } 



controllers/cpages.php


PHP Code:
public function pcategories() {  
    
        $data
['menu'] = $this->db->get('pages');
    
        
$this->load->view('pcategories'$data);  
    
    
    



views/pcategories.php


PHP Code:
<div class="row-fluid">
                <
div class="span12">
                    
                    <
button type="button" class="add" onclick="location.href='<?php echo site_url('cpages/addparentctg'); ?>';">ADD PARENT CATEGORIES</button
                    
                    <
div class="widget-box">
                        <
div class="widget-title"><h5>Parent Categories</h5></div>
                        <
div class="widget-content">
                        
                        <
table border="0" style="width: 100%; height: 90px;">
                            <
tr>
                                <
td>NAME</td>
                                <
td>DESCRIPTION</td>
                                <
td>EDIT</td>
                                <
td>DELETE</td>    
                            </
td>
                            <
tr>
                                <
td><?php echo $menu?></td>
                                <td>Mens</td>
                                <td><button type="button" class="edit" href="adminform.php">EDIT</button></td>
                                <td><button type="button" class="delete" href="adminform.php">DELETE</button></td>    
                            </td>    
                            <tr>
                                <td>Womens</td>
                                <td>Womens</td>
                                <td><button type="button" class="edit" href="adminform.php">EDIT</button></td>
                                <td><button type="button" class="delete" href="adminform.php">DELETE</button></td>    
                            </td>    
                        </table>            
                        </div>
                    </div>                    
                </div>
            </div> 


Line 53:                              <td><?php echo $menu; ?></td>


RE: class CI_DB_mysqli_result could not be converted to string - InsiteFX - 07-22-2016

Do you know the difference between a result() object and a result_array() array?


RE: class CI_DB_mysqli_result could not be converted to string - meow - 07-22-2016

Its all explained in the ci docs here

Generating Query Results
http://www.codeigniter.com/user_guide/database/results.html


In

PHP Code:
public function call_menu()
{
    
$this->db->select('*')->from('pages');

    
$query $this->db->get();

    return 
$query->result();




change 

Code:
return $query->result();
to

Code:
return $query->result_array();


This will give you an array to work with as follows from the controller

PHP Code:
public function pcategories()
{
    
$data['menu'] = $this->pages_model->call_menu();

    
$this->load->view('pcategories'$data);




And in your view you can show the results as follows

PHP Code:
foreach ($menu as $array => $row)
{
    
print_r($row);




RE: class CI_DB_mysqli_result could not be converted to string - InsiteFX - 07-22-2016

He never learns anything he just keeps asking the same error questions over and over.

Sit down and take the time to try and learn what your trying to do.

Google search is your friend.