問題描述
我使用的是 Postgresql,當我想使用 PDO 來檢索最新的插入 ID 時,我遇到了問題.這是我的代碼:
I am using Postgresql, when I want to use PDO to retrieve the latest insertion ID, I got a problem. Here is my code:
$db->lastInsertId('columnName');
錯誤信息說
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "columnName" does not exist
我想我對 PHP 手冊中所述的序列對象"有些誤解.
I guess I have some misunderstanding about "sequence object" stated in the PHP Manual.
Note:
Returns the ID of the last inserted row, or the last value from a sequence object,
depending on the underlying driver. For example, PDO_PGSQL() requires you to specify the
name of a sequence object for the name parameter.
目前,columnName"是該自增屬性的字符串.誰能指出我哪里出錯了?謝謝.
Currently, the "columnName" is the string of that auto-incremented attribute. Can anyone point out where I went wrong? Thanks.
推薦答案
PostgreSQL 使用 序列 為serial
列和 serial
列通常用于 PostgreSQL 中的自動遞增"列.序列有名稱,并且通常獨立于任何特定的表,因此您可以使用一個序列為多個不同的表生成唯一的 ID;序列名稱是 lastInsertId
想要作為其參數的名稱:
PostgreSQL uses sequences to generate values for serial
columns and serial
columns are generally what is used for "auto-incrementing" columns in PostgreSQL. Sequences have names and are, in general, independent of any particular table so you could have one sequence generating unique IDs for several different tables; the sequence name is what lastInsertId
wants as its argument:
例如,PDO_PGSQL() 要求您為 name 參數指定序列對象的名稱.
For example, PDO_PGSQL() requires you to specify the name of a sequence object for the name parameter.
PostgreSQL創建的序列對象自動命名為[table]_[column]_seq
,所以:
The sequence object created by PostgreSQL is automatically named [table]_[column]_seq
, So:
$id = $db->lastInsertId('tableName_columnName_seq');
這篇關于lastInsertId 在 Postgresql 中不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!