Object of class stdClass could not be converted to string

后端 未结 6 965
时光说笑
时光说笑 2020-12-01 11:45

I am having a problem with PHP at the moment, I am getting this error,

Object of class stdClass could not be converted to string the error occurs when

相关标签:
6条回答
  • 2020-12-01 12:23

    In General to get rid of

    Object of class stdClass could not be converted to string.

    try to use echo '<pre>'; print_r($sql_query); for my SQL Query got the result as

    stdClass Object
    (
        [num_rows] => 1
        [row] => Array
            (
                [option_id] => 2
                [type] => select
                [sort_order] => 0
            )
    
        [rows] => Array
            (
                [0] => Array
                    (
                        [option_id] => 2
                        [type] => select
                        [sort_order] => 0
                    )
    
            )
    
    )
    

    In order to acces there are different methods E.g.: num_rows, row, rows

    echo $query2->row['option_id'];
    

    Will give the result as 2

    0 讨论(0)
  • 2020-12-01 12:24

    I use codeignator and I got the error:

    Object of class stdClass could not be converted to string.

    for this post I get my result

    I use in my model section

    $query = $this->db->get('user', 10);
            return $query->result();
    

    and from this post I use

    $query = $this->db->get('user', 10);
            return $query->row();
    

    and I solved my problem

    0 讨论(0)
  • 2020-12-01 12:26

    try this

    return $query->result_array();
    
    0 讨论(0)
  • 2020-12-01 12:34

    What I was looking for is a way to fetch the data

    so I used this $data = $this->db->get('table_name')->result_array();

    and then fetched my data just as you operate on array objects.

    $data[0]['field_name']

    No need to worry about type casting or anything just straight to the point.

    So it worked for me.

    0 讨论(0)
  • 2020-12-01 12:37

    Most likely, the userdata() function is returning an object, not a string. Look into the documentation (or var_dump the return value) to find out which value you need to use.

    0 讨论(0)
  • 2020-12-01 12:37

    You mentioned in another comment that you aren't expecting your get_userdata() function to return an stdClass object? If that is the case, you should mind this line in that function:

    return $query->row();
    

    Here, the CodeIgniter database object "$query" has its row() method called which returns an stdClass. Alternately, you could run row_array() which returns the same data in array form.

    Anyway, I strongly suspect that this isn't the root cause of the problem. Can you give us some more details, perhaps? Play around with some things and let us know how it goes. We can't play with your code, so it's hard to say exactly what's going on.

    0 讨论(0)
提交回复
热议问题