0

I'm trying to wrap my head around PDO. How come $lastid doesn't output anything?

function renderRoot($db){
    $sql = "INSERT INTO nodes (name) VALUES ('/');";
    $response = $db->query($sql);
    $lastid = $db->lastInsertId();
    echo $lastid;
    return;
}

The code adds a value to the table and it has an column called id, that aoutincrements.

Here is my sql(postgresql):

$nodetable = "create table nodes (
    id serial primary key,
    parentid integer references nodes(id ),
    name varchar
);";
Cœur
  • 37,241
  • 25
  • 195
  • 267
Himmators
  • 14,278
  • 36
  • 132
  • 223

1 Answers1

0

In postgresql, lastInsertId() take an argument, in your case, the name of the sequence, i.e. nodes_id_seq

$lastid = $db->lastInsertId('nodes_id_seq');

Otherwise it will return the last oid, if any.

Cefull Lo
  • 91
  • 2