Friday, 15 June 2012

sql - How to get auto-increment Primary key of inserted row PDO PHP? -


I want to add a new line to the database using PDO, primary key is auto increment, so I Do not include the value of PK This code is:

  Public function insertion question ($ text) {try {$ sql = "Include questions (text) values ​​(questions)" ; $ Stm = $ this- & gt; Ready ($ sql); $ Stm-> Execute (array (': question' = & gt; $ text,)); $ Questions = new question (); $ Question-> Text = $ text; $ Question-> Id = -1; // How do I put the pk of the line right now? } Hold (PDOException $ E) {if ($ e-> getCode () == 1062) returned incorrect; // continuous obligation fails other echo $ e- & gt; GetMessage (); }}  

However, I need to store the PK of the new line in the $ question object, I have other features which are unique, so I can do a SELECT statement to find Can PK, however, have a better way to do this?

Call your last insert ID of PDO object.

  if ($ stmt-> execution ()) {return $ pdo-> Last InsertID (); } return false;  

No comments:

Post a Comment