Вопрос

This is how I create my handle

$this->_handle = new SQLite3($this->_dbname);

This is how I make my db query (shortened):

$stmt = $this->_handle->prepare($sql);
// execute query
$result = $stmt->execute();
// get all results
while($row = $result->fetchArray(SQLITE3_ASSOC)){
    $res[] = $row;
}

Now I want to get the last inserted ID. I tried it this way:

public function last_insert_rowid()
{
    $result = sqlite_last_insert_rowid($this->_handle);
    var_dump($result);
    echo '<pre>';
    print_r($result);
    echo '</pre>';
    return $result;
}

I get this error:

Warning: sqlite_last_insert_rowid() expects parameter 1 to be resource, object given in ... on line 444 NULL

Edit: This cannot work because I must have it opened with sqlite_open().

I also tried it with this code:

function last_insert_rowid(){
    global $db;

    $sql = "SELECT last_insert_rowid();";
    $result = $db->ExecuteQuery($sql);
    var_dump($result);
    return $result;
    //return ($db->last_insert_rowid());
}

I get the following error message:

NULL Warning: SQLite3Stmt::execute() [sqlite3stmt.execute]: Unable to execute statement: constraint failed in ..

The error points to this line $stmt->execute();

How do I get my last inserted ID?

Это было полезно?

Решение

The function has another name lastInsertRowID. Solution:

public function last_insert_rowid()
{
    //$result = sqlite_last_insert_rowid($this->_handle);
    $result = $this->_handle->lastInsertRowID();

    return $result;
}

Другие советы

It looks to me like the query failed to execute. I would double check the query that's being run. Maybe you are setting a NOT NULL coulmn to NULL as indicated by Unable to execute statement: constraint failed in

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top