-2

Possible Duplicate:
Can't get mysql_insert_id() to work

I want to print the last inserted id using lastInsertId() on this function:

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>";

}

I have added this:

$cid = parent::lastInsertId();

and then added $cid here:

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

But I keep getting:

Fatal error: Call to undefined method Generic::lastInsertId() in

The database connection looks like this:

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>';
    }


}

Any ideas how I should properly add lastInsertId() ?

Community
  • 1
  • 1
Dreni
  • 161
  • 1
  • 5
  • 18
  • 1
    what framework/class you are using for DB? – rkosegi Sep 19 '12 at 11:39
  • raina77ow, yes since I discovered that this has nothing to do with mysql_insert_id() since this is PDO. – Dreni Sep 19 '12 at 11:45
  • raina77ow, sorry this is not Magento, this is a user login script which im trying to modify by adding lastInsertId(), but I can't get it to work. – Dreni Sep 19 '12 at 11:51
  • 1
    What you basically need is to call lastInsertId() method on your connection object. From the snippets shown it looks like it's stored in `self::$dbh`, so probably it should be used like this: `self::$dbh->lastInsertId()` – raina77ow Sep 19 '12 at 11:51
  • raina77ow, thanks a million, it worked, wow. You are my hero, thanks once again. – Dreni Sep 19 '12 at 11:52

2 Answers2

2

Replace

$cid = parent::lastInsertId();

with

self::$dbh->lastInsertId();

... as (it looks like) your connection object is stored in the dbh field of this anonymous class we're talking about.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

LastInsertID is a method of PDO class.

You are calling it on the Generic class, that has no method LastInsertID();

JvdBerg
  • 21,777
  • 8
  • 38
  • 55