1

I am working with codeigniter ona PHP project at the moment, and one the libraries I am using returns an object of database results, that looks like this,

    stdClass Object
(
    [user_id] => 2
    [firstname] => John
    [surname] => Doe
    [email] => [email protected]
    [password] => 9d210a03bed920971fefab01b8f84a27
    [postcode] => W1 5ST
    [date_registered] => 2021-04-11 10:56:47
    [last_logged_in] => 2011-05-04 11:56:03
    [user_type_id] => 2
)

I am then trying to echo the values using the follow code, I sending user to my view by doing the following,

$data['users'] = $this->users_model->get_by($this->session->userdata('user_id');
$this->load->view('view', $data);

Then in my view I am doing the following,

echo $users->firstname;

However I get the following error at the top of my page,

A PHP Error was encountered

Severity: 4096

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

Filename: libraries/Parser.php

Line Number: 63

How can I resolve this? Without changing how the database results are returned ideally.

2
  • "$data['users']" and "echo $user->firstname;" doesn't match. Just a misspell or is it the problem? Commented May 4, 2011 at 12:10
  • You're assigning to users yet accessing user. Is that a typo? Commented May 4, 2011 at 12:11

4 Answers 4

1

You cannot echo an object directly. create a method __toString() or serialize the object before echoing.

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

Comments

1

You're on the right track. A few things.

You're missing a closing ) in this line.

$data['users'] = $this->users_model->get_by($this->session->userdata('user_id'));

Also, It's hard to know what is being returned by your users model. Can you provide the code for $this->users_model->get_by()? That's where your problem will be.

Make sure you have your db query returning the object as you expect. You may need to add ->row() .

Comments

0

maybe it's because you're sending a variable called "users" to your view.. but in your view you call the variable $user and not $users

if not... the doc on views says:

Note: If you use an object, the class variables will be turned into array elements.

Good Luck!

1 Comment

yah doc says that, but what does it really mean ? how to access those variables ?
0

For Codeigniter, you need to call $users->result(), $users->result_array(), $users->row() or $users->row_array() to access the DB result object data

http://codeigniter.com/user_guide/database/results.html

Assuming a single row is returned:

$data['users'] = $this->users_model->get_by($this->session->userdata('user_id');
$data['users'] = $data['users']->row();
$this->load->view('view', $data);

Comments

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.