重複の可能性:
mysql_insert_id() が機能しない
この関数でlastInsertId()を使用して、最後に挿入された 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');
$cid = parent::lastInsertId();
if(!parent::sendEmail($this->email, $subj, $msg, $shortcodes))
$this->error = _('ERROR. Mail not sent');
$this->result = "<div class='alert alert-success'>" . sprintf(_('Successfully added user <b>%s</b> to the database. Credentials sent to user.'), $cid) . "</div>";
}
これを追加しました:
$cid = parent::lastInsertId();
そして、ここに$cidを追加しました:
$this->result = "<div class='alert alert-success'>" . sprintf(_('Successfully added user <b>%s</b> to the database. Credentials sent to user.'), $cid) . "</div>";
しかし、私は取得し続けます:
Fatal error: Call to undefined method Generic::lastInsertId() in
データベース接続は次のようになります。
public function dbConn() {
include(dirname(__FILE__) . '/config.php');
try {
self::$dbh = new PDO("mysql:host={$host};dbname={$dbName}", $dbUser, $dbPass);
self::$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
} catch (PDOException $e) {
return '<div class="alert alert-error">'._('Database error: '). $e->getMessage() . '</div>';
}
}
lastInsertId()を適切に追加する方法はありますか?