Welcome Guest, Not a member yet? Register   Sign In
easy way (!) to convert result of get() into a string?
#1

[eluser]sparbier[/eluser]
I merely want to put the users email-address on the page, so I query the database only for the e-mail field by comparing with the id in the sessions userdata, which results in one single field in one single row.
Now that query result is in the special format of CI, i.e. apparantly I can not directly treat it just as an array (error reads something like "cannot treat CI_DB... as array") or straightforward as a string.

The code below does work, but it looks a bit stupid to me to go through a foreach if I know already that there is only one row.

any suggestions?
Code:
$i = $this->session->userdata('id');
     $this->db->select('email')->from('users')->where('id', $i);
     $me = $this->db->get();
    foreach ($me->result() as $row)
        {
                echo $row->email;
        }
#2

[eluser]LDMajor[/eluser]
Code:
$i = $this->session->userdata('id');
     $this->db->select('email')->from('users')->where('id', $i);
     $me = $this->db->get();
     $row=$me->row();
     echo $row->email

you always can write a function that will do it for you =\
#3

[eluser]Jamie Rumbelow[/eluser]
Instead of running result() on your get() object, run row() and that will return the object directly.
#4

[eluser]dcunited08[/eluser]
[quote author="Jamie Rumbelow" date="1225404470"]Instead of running result() on your get() object, run row() and that will return the object directly.[/quote]

Code:
$i = $this->session->userdata('id');
     $this->db->select('email')->from('users')->where('id', $i);
     $me = $this->db->get();
     $row=$me->row();
     echo $row->email

if you are using PHP5:
    echo $me->row()->email;
or even:
    echo $this->db->get()->row()->email;  //never tried but should work

I would add a test to make sure that there is only one result.
#5

[eluser]sparbier[/eluser]
thanks all!
Code:
echo $this->db->get()->row()->email;
works great, thank you!




Theme © iAndrew 2016 - Forum software by © MyBB