1
votes
function random()
    {
    $anketadb = $this->load->database('anketa',TRUE);
    $br = $anketadb->count_all_results('anketadata');
    $nmb = mt_rand(1,$br);

    if ($nmb != 1){
    $nmb = $nmb - 1;
    }

    $count = $anketadb->get('anketadata', 1, $nmb);

    return $count;
    }

Why this code when i echo it in View returns ERROR:

A PHP Error was encountered

Severity: 4096

Message: Object of class CI_DB_mysqli_result could not be converted to string

Filename: ankete/rezultatiankete.php

Line Number: 52

2

2 Answers

1
votes

You should have shown us more code (controller, view etc), anyways, in your example you are using

return $count;

in this case $count; is an object and to echo it's fields you have to loop in your view like

foreach ($count->result() as $row)
{
    echo $row->fieldname; // rerplace the fieldname with a real field/column name of your database
}

so if you are trying to echo the $count then you are making a mistake, it's an object, read more here.

0
votes

echo doesnot work for objects use print_r(returned value) to output objects,arrays

to fetch a random column use SELECT * FROM table ORDER BY RAND() LIMIT 0,1;