0

我试过这个:

$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 资源

这是整个函数(我添加了这个$cid = mysql_insert_id($this->result);希望检索插入数据的 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"; 
?>
4

3 回答 3

2

mysql_insert_id()将资源链接(即数据库句柄)作为参数而不是查询结果。包含什么$this->result

于 2012-09-19T07:26:45.303 回答
1

检查手册

参数为mysql_insert_idLink_Identifier。但我猜你正在传递结果。尝试

$cid = mysql_insert_id();

或者你的 $this->result 必须是 mysql_connect() 返回的连接标识符。

于 2012-09-19T07:28:42.323 回答
0

您已经在使用 PDO,请切换到:

$cid = self::$dbh->lastInsertId(); // PDO for mysql_insert_id
于 2012-09-19T07:33:22.300 回答