php-CodeIgniter-类CI_DB_mysqli_result的对象无法转换为字符串

我目前正在使用codeIgniter.我正在尝试根据电子邮件地址更新数据库中的密码.否则,我会遇到问题,请使用以下代码更新在CodeIgniter网站上找到的数据库.

$this->db->set('field', 'field+1', FALSE);
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2

我收到的错误消息是

Object of class CI_DB_mysqli_result could not be converted to string

我在互联网上发现了很多谈论这种问题的话题.但是,我没有找到适合我的情况的任何内容.如前所述,我已经尝试过像这样返回查询结果

if ($query->num_rows() == 1)
{
    return $query->result();
}
else
    return FALSE;

正如您所猜测的,这没有改变任何东西.所以希望有人能够向我解释发生了什么.这是我的模型代码.我没有写完整的代码,因为我觉得它足够满足我的需求,但是问我是否还需要其余的代码.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Reset_password extends CI_Model
{
    function __construct()
    {
        parent:: __construct();
        $this->load->helper('url');
        $this->load->helper('string');
    }

    function index($email) //function wchich will reset the password in the database;
    {
        $new_password = random_string('alnum', 16);
        //generate random password, already try to remove and put for exemple
        //$new_password = 'lol'; and doesn't work so I suppose this line isn't 
        //the pb
        $this-> db->set('password', md5($new_password), FALSE);
        $this-> db->where('email', $email);
        $this-> db->update('CI_TEST');

        $query = $this->db->get();
    }
}

谢谢 :)

编辑:这是我的控制器代码

<?php if (!defined('BASEPATH')) exit('No direct script acess allowed');

class Forget_password extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form'));
        $this->load->model('Reset_password');
        $this->load->library('email');
    }

    function index()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_email_check', 'Error: please provide a valide email adresse');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('Forget_password');
        }
        else
        {
           //loading
        }
    }

    function email_check($email)
    {
        $this-> db->select('email');
        $this-> db->from('CI_TEST');
        $this-> db->where('email', $email);
        $this-> db->limit(1);

        $query = $this-> db->get();

        if ($query->num_rows() == 1)
        {           
            $this->Reset_password->index($query);
            return (TRUE);
        }
        else
        {
            echo 'Error: The email you provided doesn\'t exist in the database';
            return (FALSE);
        }
    }
}
?>

解决方法:

当前,您已经获取了一个结果集,然后需要使用result()或row()将结果收集到格式化的对象中(如果选择result_array(),则为数组). CI_DB_mysqli_result是一个类包装程序,用于包装结果,但尚未提取其中包含的数据.

$this->db->select('email');
$this->db->from('CI_TEST');
$this->db->where('email', $email);
$this->db->limit(1);

$query = $this->db->get();

if ($query->num_rows() == 1)
{           
    //Use row() to get a single result
    $row = $query->row();

    //$row will now have if you printed the contents:
    //print_r( $row );
    //stdClass Object ( [email] => example@gmail.com )

    //Pass $query->email directly to reset_password
    $this->Reset_password->index( $row->email );
    return true;
}
上一篇:php-代码点火器-如果查询结果返回行,如何更新数据库


下一篇:php-将解析API与codeigniter一起使用