我目前正在使用 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
我收到的错误消息是
CI_DB_mysqli_result 类的对象无法转换为字符串
我在互联网上发现了很多讨论此类问题的话题。然而,我没有发现任何适合我的情况。正如我之前读到的,我已经尝试像这样返回查询结果
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);
}
}
}
?>