kim: Letzte ID wird nicht ausgelesen

Hallo,
Was ist falsch an der select Abfrage. Möchte die ID des letzten gespeicherten datensatzes auslesen. Kriegs aber nicht hin. WARUM???

$sqlInsert  = "INSERT INTO test (version) VALUES ('".mysql_escape_string($version)."')";
$result = "SELECT id FROM test WHERE id=LAST_INSERT_ID()";
echo $id;

Danke kim

  1. Hallo Kim,

    ein Funktionsaufruf muss außerhalb einer Zeichenkette stehen:

    $result = "SELECT id FROM test WHERE id=".LAST_INSERT_ID();
    echo $id;

    MfG, Kay

    1. Hello!

      LAST_INSERT_ID([expr])

      Returns the last automatically generated value that was inserted into an AUTO_INCREMENT column.

      mysql> SELECT LAST_INSERT_ID();
              -> 195

      The last ID that was generated is maintained in the server on a per-connection basis.  It will not be changed by another client. It will not even be changed if you update another AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not 0).

      If you insert many rows at the same time with an insert statement, LAST_INSERT_ID() returns the value for the first inserted row.  The reason for this is so that you it makes it possible to easily reproduce the same INSERT statement against some other server.

      If expr is given as an argument to LAST_INSERT_ID(), then the value of the argument is returned by the function, and is set as the next value to be returned by LAST_INSERT_ID().  This can be used to simulate sequences:

      First create the table:

      mysql> CREATE TABLE sequence (id INT NOT NULL);
      mysql> INSERT INTO sequence VALUES (0);

      Then the table can be used to generate sequence numbers like this:

      mysql> UPDATE sequence SET id=LAST_INSERT_ID(id+1);

      You can generate sequences without calling LAST_INSERT_ID(), but the utility of using the function this way is that the ID value is maintained in the server as the last automatically generated value (multi-user safe).  You can retrieve the new ID as you would read any normal AUTO_INCREMENT value in MySQL. For example, LAST_INSERT_ID() (without an argument) will return the new ID.  The C API function mysql_insert_id() can also be used to get the value.

      Note that as mysql_insert_id() is only updated after INSERT and UPDATE statements, so you can't use the C API function to retrieve the value for LAST_INSERT_ID(expr) after executing other SQL statements like SELECT or SET.

    2. Hallo Kay,
      Wenn ichs so mache wie Du mir das beschrieben hast bekomme ich folgende Fehlermeldung.

      Fatal error: Call to undefined function: last_insert_id() in /var/www/htdocs/andi/insert.php on line 11

      kim

      1. Hallo Kay,
        Fatal error: Call to undefined function: last_insert_id() in /var/www/htdocs/andi/insert.php on line 11

        kim

        Hallo

        Das ist ganz normal, weil last_index_id() keine funktion unter php ist es sei denn du hast diese mal selber definiert....

        LAST_INDEX_ID() ist eine MySQL-Funktion

        vG

        Sascha

        1. Sorry ich meine "last_INSERT_id()" !

          Das ist ganz normal, weil last_index_id() keine funktion unter php ist es sei denn du hast diese mal selber definiert....
          LAST_INDEX_ID() ist eine MySQL-Funktion
          vG

          Sascha