我在PHP中使用sqlite_last_insert_rowid时遇到问题。 代码:
$database = new SQLiteDatabase('example.db');
$sql_1 = "INSERT INTO ex1(test1, test2) VALUES ('$test1','$test2')";
$database->queryExec($sql_1);
$ex1_ID = sqlite_last_insert_rowid($database);
$sql_2 = "INSERT INTO ex2 (ex1_ID, fname, lname) values ('$ex1_ID','$fname','$lname');";
$database->queryExec($sql);
我想将ex1表中的ID存储到ex1_ID
中的表ex2CREATE TABLE ex1 (
ID INTEGER PRIMARY KEY,
test1 LONGTEXT,
test2 LONGTEXT
);
CREATE TABLE ex2 (
ID INTEGER PRIMARY KEY,
ex1_ID INTEGER,
fname LONGTEXT,
lname LONGTEXT,
FOREIGN KEY ( ex1_ID ) REFERENCES ex1 ( ID )
);
完整代码:
$database = new SQLiteDatabase('example.db');
for ($i = 0; $i < 10; $i++) {
$test1 = "";
$test2 = "";
$lenght = rand(300, 400);
for ($x = 0; $x < $lenght; $x++) {
$test1 .= rand(0, 9);
$test2 .= rand(0, 9);
}
$sql_1 = "INSERT INTO ex1 (test1,test2) VALUES ('$test1','$test2')";
$database->queryExec($sql_1);
$ex1_ID = sqlite_last_insert_rowid($database);
for ($j = 0; $j < 3; $j++) {
for ($x = 0; $x < $lenght; $x++) {
$test1 .= rand(0, 9);
$test2 .= rand(0, 9);
}
$sql_2 = "INSERT INTO ex2 (ex1_ID, fname, lname) values ('$ex1_ID','$test1','$test2');";
$database->queryExec($sql_2);
}
}
我无法从ex1表中获取最后一个ID。 请帮忙!
答案 0 :(得分:2)
现在我们有了更多信息。
由于您使用对象模型方式查询数据库,您是否尝试过
$lastid = $database->lastInsertRowid();