将 lastinsertid 分配给变量

assign lastinsertid to a variable

我基本上是一名新程序员,过去几年有一些 Oracle 经验。我有一个收集家庭和成员信息的表格(它还有一个带有数据库连接的下拉列表,该数据库连接连接到数据库以检索分支列表框)。

在POST上,将table的住户和成员信息保存在数据库中。住户是parenttable,所以必须先创建记录。然后我需要检索 household.id(一个 MySql 自动递增字段)以保存到成员 table,以将两者相关联。

在执行插入到家庭 table 时,它工作正常。我打印了自动增量字段来检查。

    $dbh->beginTransaction();
     $stmt->execute();
     print("lastinsertid is really = \n");
  //   $lastid->$dbh->lastInsertId();
     print $dbh->lastInsertId();

但是我需要在下一个语句中使用它所以需要存储它。当我这样做时(只需更改注释掉的内容),我就会出错。

     $dbh->beginTransaction();
     $stmt->execute();
     print("lastinsertid is really = \n");
     $lastid->$dbh->lastInsertId();
  //   print $dbh->lastInsertId();

错误:

PHP Notice: Undefined variable: lastid in C:\Users\Desktop\OECC\form with lastinsertid works.php on line 165
PHP Notice: Trying to get property of non-object in C:\Users\Desktop\OECC\form with lastinsertid works.php on line 165 PHP Fatal error: Call to a member function lastInsertId() on null in C:\Users\Desktop\OECC\form with lastinsertid works.php on line 165

$lastid = $dbh->lastInsertId()

您正在使用 -> 而不是 =