Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> PDO::lastInsertId() zwraca true?
czachor
post 21.01.2007, 12:18:42
Post #1





Grupa: Zarejestrowani
Postów: 897
Pomógł: 40
Dołączył: 16.12.2003
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Mam problem... albo jakieś zamrocznie...

  1. <?php
  2. $stmt = $this->_dbHandler->prepare('INSERT INTO news (' . $startDateField . $expireDateField . ' autodelete,' . $imgIntroField . ' user_id, author_name)
  3. VALUES (' . $startDateValue . $expireDateValue . $autodelete . ', ' . $imgIntroValue . ' NULL, '' . $author . '')');
  4.  
  5. if ($stmt->execute() == true)
  6. {
  7. echo '(debug) lastID: ' . $this->_dbHandler->lastInsertId();
  8. return true;
  9. }
  10. else
  11. {
  12. return false;
  13. };
  14. ?>


Całość oczywiście PDO. Zapytanie działa, rekord jest dodawany do bazy. Klucz główny jest na polu 'news_id' (dodawany automatycznie na poziomie bazy - typ SERIAL w postgresie).

lastInsertId" title="Zobacz w manualu php" target="_manual (linia 7) zwraca mi true, a powinno ID (news_id) dodanego właśnie rekordu. Nie wiem już czemu... Czy jest jakieś wymaganie, żeby pole z kluczem głównym nazywało się 'id'? A jeśli tak, to jak to obejść?

Z góry dzięki za pomoc.

--update
próbowałem też użyć RETURNING przy INSERT, ale postgres zwraca błąd (ERROR: syntax error at or near "RETURNING" at character 89).

--update2

Narazie to obszedłem w ten sposób:

  1. CREATE OR REPLACE FUNCTION getLastNewsId()
  2. RETURNS int
  3. AS $$
  4. BEGIN RETURN currval('news_news_id_seq');
  5. END;
  6. $$ LANGUAGE plpgsql VOLATILE;


i po INSERT dodałem zapytanie, które zwraca mi ostatnie wprowadzone news_id:
  1. SELECT nextNewsId()


Trochę naokoło, dlatego będę wdzięczny, jeśli ktoś będzie wiedział, co zrobić z tym PDO...

Ten post edytował czachor 21.01.2007, 12:55:13


--------------------
how many SEO experts does it take to change a light bulb,lightbulb,light,bulb,lamp,lighting,switch,sex,xxx
5-Reasons-why-you-should-NEVER-fix-a-computer-for-free
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
sf
post 21.01.2007, 18:41:27
Post #2





Grupa: Zarejestrowani
Postów: 1 597
Pomógł: 30
Dołączył: 19.02.2003
Skąd: Tychy

Ostrzeżenie: (0%)
-----


Cytat
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.


--------------------
Zapraszam na mój php blog, tworzenie stron.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 24.07.2025 - 20:08