1
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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, inside the foreach just echo the column name like echo $row->fieldname;
But this is in Model , so in View can you show me how to do it?
Are you loading it from a controller ?
Yes, I am loading view from controller, the controller part is not a problem
0

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;

3 Comments

If i use print_r its print something like this: 'CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 1 [client_info] => 5.1.52 [client_version] => 50152 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [field_count] => 15 [host_info] => Localhost via UNIX socket [info] => [insert_id] => 0 [server_info] => 5.1.61-log [server_version] => 50161 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 18494924 [warning_count] => 0 ) [resul.....'
what details you want from database
SELECT * FROM table ORDER BY RAND() LIMIT 0,1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.