严重性:4096消息:类CI_DB_mysql_result的对象无法转换为字符串

时间:2014-05-29 08:36:53

标签: php mysql arrays codeigniter

我无法获得查询的字符串结果....我该如何解决?

型号:

class Get_info extends CI_Model {

  function Get_info() {
    parent::__construct(); 
    $this->load->database();

  }


  function getFirstName($email){
        $query_id=$this->db->query("SELECT user_id FROM user WHERE email='$email'");
        $query=$this->db->query("SELECT firstname FROM `user_info` WHERE id='$query_id'");
        var_dump($query->row());
        return $query->result();

  }
}

控制器:

$email = $this->input->post('email');
$this->load->model('get_info'); 
$result = $this->get_info->getFirstName($email);
$profile = $result[0];

$newdata = array(
        'email' =>  $email,
        'firstname' => $profile
        );
$this->load->library('session');
$this->session->set_userdata( $newdata );

当我在modelit中调用var_dump($ query)时,请回答我:object(CI_DB_mysql_result)#23 (8) { ["conn_id"]=> resource(62) of type (mysql link persistent) ["result_id"]=> resource(91) of type (mysql result) ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> int(0) ["row_data"]=> NULL }

当我在控制器中调用var_dump($ profile)时,它会回答我:array(0) { }

我的错误在哪里?我只想从查询中获取字符串文本并将其放在数据会话中。

1 个答案:

答案 0 :(得分:1)

$query=$this->db->query("SELECT firstname FROM {USER_INFO {1}}

此处WHERE id='$query_id'");是上一个查询的结果,而不是 INT

这样做保护您的查询

$query_id

您也可以使用AR,甚至可以创建唯一查询:

$query_id = $this->db->query("SELECT user_id FROM user WHERE email=?", array($email))->row();

$query=$this->db->query("SELECT firstname FROM `user_info` WHERE id=?", array($query_id->user_id))->result();

编辑上面的查询并将return $this->db->select('user_info.firstname, user.email') ->from('user_info') ->join('user', 'user.user_id = user_info.id') ->where('user.email', $email) ->get() ->row(); 更改为result(),因为您只查找一条​​记录。现在您的其他代码也必须更改:

row()