PDO Last Insert ID

Discussion in 'Databases' started by scottlpool2003, Mar 1, 2013.

  1. #1
    Tried quite a few ways but always returns:

    Any ideas?

    EDIT
    Using MSSQL and mainly getting not supported by driver error.

    Is there any way I can incorporate getting the last ID without running a SELECT query?

    if(isset($_POST['createpub']))  {
     
    //Create publication
      $sth = $dbconn->prepare("INSERT INTO publication (title, text, cat, user_id, use_ad, viewcount, tags) VALUES (:title, :text, :cat, :user_id, :use_ad, :viewcount, :tags)");
      $params = array(
      "title" => $_POST[title],
      "text" => $_POST[description],
      "cat" => $_POST[category],
      "user_id" => $userid,
      "use_ad" => 0,
      "viewcount" => 0,
      "tags" => $_POST[keywords]
      );
      $status = $sth->execute($params);
     
    if ($status) {
      //Create folder using insert id
      echo $sth->lastInsertId();
     
      //mkdir("../publication/$userid/$sth->lastInsertId()");
    } else {
      //Failed
      //Debug test
      print_r($params);
    }
    PHP:
     
    Last edited: Mar 1, 2013
    scottlpool2003, Mar 1, 2013 IP
  2. Garkoni

    Garkoni Active Member

    Messages:
    213
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    If I'm not mistaken, you should use:
    echo $dbconn->lastInsertId();
    PHP:
     
    Garkoni, Mar 6, 2013 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Tried many different variations. Always end up with unsupported driver error though.

    Although the driver I have is MSSQL_70


    This should work shouldn't it?
     
    scottlpool2003, Mar 7, 2013 IP