无法获得mysql_insert_id()工作(Can't get mysql_insert

2019-07-31 16:56发布

我尝试这样做:

$cid = mysql_insert_id($this->result);
$this->result = "<div class='alert alert-success'>" . sprintf(_('Successfully added user id <b>%s</b> to the database.'), $cid) . "</div>";

但我只得到这个错误:

警告:mysql_insert_id():提供的参数不是一个有效的MySQL-Link的资源在

这是整个功能(I加入此$ CID = mysql_insert_id($这个- >结果);希望检索插入的数据的ID):

private function adduser() {

    if (!empty($this->error)) return false;

    $params = array(
        ':user_level' => parent::getOption('default-level'),
        ':name'       => $this->name,
        ':email'      => $this->email,
        ':username'   => $this->username,
        ':password'   => parent::hashPassword($this->password)
    );

    parent::query("INSERT INTO `login_users` (`user_level`, `name`, `email`, `username`, `password`)
                    VALUES (:user_level, :name, :email, :username, :password);", $params);

    $shortcodes = array(
        'site_address'  =>  SITE_PATH,
        'full_name'     =>  $this->name,
        'username'      =>  $this->username,
        'email'         =>  $this->email,
        'password'      =>  $this->password
    );

    $subj = parent::getOption('email-add-user-subj');
    $msg = parent::getOption('email-add-user-msg');

    if(!parent::sendEmail($this->email, $subj, $msg, $shortcodes))
        $this->error = _('ERROR. Mail not sent');
    $cid = mysql_insert_id($this->result);
    $this->result = "<div class='alert alert-success'>" . sprintf(_('Successfully added user <b>%s</b> to the database. Credentials sent to user.'), $cid) . "</div>";

}

这是与数据库的连接:

<?php
$host = "xxx"; 
$dbName = "xxx"; 
$dbUser = "xxx"; 
$dbPass = "xxx"; 
?>

Answer 1:

mysql_insert_id()需要一个资源链接,即数据库句柄,作为参数不是查询结果。 这是什么$this->result包含哪些内容?



Answer 2:

检查手册

参数mysql_insert_id是Link_Identifier。 但我猜你是路过的结果。 尝试

$cid = mysql_insert_id();

或者您$这个 - >结果必须是由mysql_connect()返回的连接标识符。



Answer 3:

你已经在使用PDO,切换到:

$cid = self::$dbh->lastInsertId(); // PDO for mysql_insert_id


文章来源: Can't get mysql_insert_id() to work