mysqli_insert_id() 需要 1 个参数,CodeIgniter 中给出的 0

muh*_*tya 2 php codeigniter

我有问题,请帮助我。我正在使用代码点火器这是我的代码。

我的模型(M_register.php)

class M_register extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function add_account($data)
    {
        $this->load->database();
        $this->db->insert('login',$data);           
        **return  mysqli_insert_id();**
    }

    function changeActiveState($key)
    {
        $this->load->database();
        $data = array(
           'active' => 1
        );

        $this->db->where('md5(id)', $key);
        $this->db->update('login', $data);

        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是控制器 $data = array( 中的部分代码

        'username' => $username,
        'password' => $password,
        'nama' => $nama,
        'email' => $email,
        'active' => 0
    );
    $this->load->model('m_register');
    **$id = $this->m_register->add_account($data);**        
    $encrypted_id = md5($id);
enter code here
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

在此输入图像描述

Stn*_*ire 5

错误消息很明确: mysqli_insert_id() 需要 1 个参数,给定 0 个参数

您必须在参数中为其提供连接。例如 :

$con = mysqli_connect("localhost","my_user","my_password","my_db");

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");

// Here the call to "mysqli_insert_id" takes the output of "mysqli_connect" as parameter
echo "New record has id: " . mysqli_insert_id($con); 

mysqli_close($con);
Run Code Online (Sandbox Code Playgroud)

来源:https://www.w3schools.com/php/func_mysqli_insert_id.asp

我不知道您的代码中在哪里进行连接,但请检查您的调用$this->load->database()是否未返回连接资源。