【问题标题】:CodeIgniter - Object of class CI_DB_mysqli_result could not be converted to stringCodeIgniter - CI_DB_mysqli_result 类的对象无法转换为字符串
【发布时间】:2015-09-22 12:03:26
【问题描述】:

我目前正在使用 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);
        }
    }
}
?>

【问题讨论】:

  • 在您的视图/控制器中,您是否尝试echo $query?其次,更新不会返回除真/假以外的任何内容,您必须重新选择要“返回”的行,而我认为在这里,您正在更新并尝试返回一组行受影响。
  • 试试 var_dump($query) 看看结果如何
  • @MackieeE 你好,不,但我编辑了我的原始帖子,如果你愿意,可以添加我的控制器代码
  • AND 也是 var_dump($this->Reset_password->index($query));
  • @AnmolRaghuvanshi var_dump($query) 显示“bool(false)”和 var_dump($this->Reset_password->index($query)) 显示“NULL”

标签: php codeigniter


【解决方案1】:

您当前已经获取了一个结果集,然后您需要使用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;
}

【讨论】:

  • 消息好像消失了! :D 我没有使用以下行“stdClass Object ([email] => example@gmail.com)”但是你能解释一下发生了什么吗?
  • @S7_0 stdClass Object ([email] =&gt; example@gmail.com) 只是一个示例,如果您使用 print_r( $row )$row 的内容将是什么,我将对其进行编辑:)
猜你喜欢
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
相关资源
最近更新 更多