返回最后插入的自动增量ID php

时间:2014-02-10 04:13:11

标签: php mysql database

我没有插入任何值但是自动更新了一列,我怎么能得到最后一个插入的id值?

我使用了echo mysql_insert_id();,但我给了'0'这似乎不正确。

2 个答案:

答案 0 :(得分:0)

只有在将数据插入表中时,

mysql_insert_id()才有效。

答案 1 :(得分:0)

  1. 数据库

    创建表MyGuests( id INT(6)UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30)NOT NULL, lastname VARCHAR(30)NOT NULL, 电子邮件VARCHAR(50), reg_date TIMESTAMP )   最后插入的ID:

    1. 示例(MySQLi面向对象)
    2. <?php
      $servername = "localhost";
      $username = "username";
      $password = "password";
      $dbname = "myDB";
      
      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      }
      
      $sql = "INSERT INTO MyGuests (firstname, lastname, email)
      VALUES ('John', 'Doe', 'john@example.com')";
      
      if ($conn->query($sql) === TRUE) {
          $last_id = $conn->insert_id;
          echo "New record created successfully. Last inserted ID is: " . $last_id;
      } else {
          echo "Error: " . $sql . "<br>" . $conn->error;
      }
      
      $conn->close();
      ?>
      
      1. 示例(MySQLi程序)
      2. <?php
        $servername = "localhost";
        $username = "username";
        $password = "password";
        $dbname = "myDB";
        
        // Create connection
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        
        $sql = "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('John', 'Doe', 'john@example.com')";
        
        if (mysqli_query($conn, $sql)) {
            $last_id = mysqli_insert_id($conn);
            echo "New record created successfully. Last inserted ID is: " . $last_id;
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
        }
        
        mysqli_close($conn);
        ?>
        
        1. 示例(PDO)
        2. <?php
          $servername = "localhost";
          $username = "username";
          $password = "password";
          $dbname = "myDBPDO";
          
          try {
              $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
              // set the PDO error mode to exception
              $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $sql = "INSERT INTO MyGuests (firstname, lastname, email)
              VALUES ('John', 'Doe', 'john@example.com')";
              // use exec() because no results are returned
              $conn->exec($sql);
              $last_id = $conn->lastInsertId();
              echo "New record created successfully. Last inserted ID is: " . $last_id;
              }
          catch(PDOException $e)
              {
              echo $sql . "<br>" . $e->getMessage();
              }
          
          $conn = null;
          ?>