0

I tried this:

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

but I only get this error:

Warning: mysql_insert_id(): supplied argument is not a valid MySQL-Link resource in

This is the entire function (i added this $cid = mysql_insert_id($this->result); hoping to retrieve the id of the inserted data):

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

}

This is the connection to the database:

<?php
$host = "xxx"; 
$dbName = "xxx"; 
$dbUser = "xxx"; 
$dbPass = "xxx"; 
?>
Dreni
  • 161
  • 1
  • 5
  • 18
  • We would need to see a few preceeding lines - especially as at the end you set $this->result to text – BugFinder Sep 19 '12 at 07:24
  • Where's your query and designated result? Please updat with some more lines of code. First off, using `this->result` as the result identifier and then using it to set a string doesn't seem correct. – bartvanraaij Sep 19 '12 at 07:25

3 Answers3

2

mysql_insert_id() takes a resource link, i.e. database handle, as an argument not the query result. What does $this->result contain?

mbfisher
  • 306
  • 3
  • 7
1

Check Manual

Parameter for mysql_insert_id is Link_Identifier. But I guess you are passing result. Try

$cid = mysql_insert_id();

or your $this->result must be connection identifier returned by mysql_connect().

Kapil Sharma
  • 10,135
  • 8
  • 37
  • 66
  • Resulted in: Warning: mysql_insert_id() [function.mysql-insert-id]: A link to the server could not be established in – Dreni Sep 19 '12 at 07:31
0

You are already using PDO, switch to:

$cid = self::$dbh->lastInsertId(); // PDO for mysql_insert_id
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107