为什么我不能在PHP和MySQL中获取最后一个插入ID?

时间:2012-03-29 18:49:39

标签: php mysqli last-insert-id

我有一个创建课程的功能。我想获取最后一个插入ID,但它不起作用:

public  function createCourse()
{
    require "/mysqli_connect.php";
    $course_q = "INSERT INTO course (....) VALUES (.....)";

    $course_r = mysqli_query($mysqli, $course_q);
    $course_n = mysqli_affected_rows($mysqli);
    if($course_n == 1)
    {
        mysqli_close($mysqli);
        return true;
    }
    mysqli_close($mysqli);
    return false;
}

这是检索我在与createCourse函数相同的类中创建的最后一个插入ID的函数:

public function getLastInsertID()
{
    require "/../mysqli_connect.php";

    $course_q= "SELECT LAST_INSERT_ID()";

    $course_r = mysqli_query($mysqli, $course_q);
    $course_n = mysqli_num_rows($course_r);

    //var_dump($course_n);
    if($course_n)
    {
        $c = mysqli_fetch_assoc($course_r);
        mysqli_close($mysqli);

        return $c;
    }
    mysqli_close($mysqli);
    return NULL;
}   

这就是我调用函数的方式:

require "/mysqli_connect.php";
$course = new Course();
$c = $course->createCourse();
$id = $course->getLastInsertID();
var_dump($id);

"$id"始终为"int(0)"

我也试过了:

require "/mysqli_connect.php";
$course = new Course();
$c = $course->createCourse();
**$id = mysqli_insert_id($mysqli);**

我也尝试过:

$course_q= "SELECT LAST_INSERT_ID() from course";

但这不起作用。你们能看出问题所在吗? :(函数createCourse本身很好。它创建了我需要的东西,它在数据库中,但我无法获得最后一个插入ID。

3 个答案:

答案 0 :(得分:3)

虽然使用MySQLi检索插入ID的正确方法是使用mysqli_insert_id(),但由于您正在进行关联提取,因此需要LAST_INSERT_ID()的列别名

 $course_q= "SELECT LAST_INSERT_ID() AS insert_id";

 // Later...
 $c = mysqli_fetch_assoc($course_r);
 echo $c['insert_id'];

但是,由于您已在mysqli_close()功能中关闭了与createCourse()的关联,因此无效。相反,在createCourse()中获取插入ID并将其返回。

public  function createCourse()
{
    // Note: You should not establish the connection in each function call.
    // it only needs to be done once per script, and you can pass the connection
    // into the class constructor or into methods that use it.
    require "/mysqli_connect.php";
    $course_q = "INSERT INTO course (....) VALUES (.....)";

    $course_r = mysqli_query($mysqli, $course_q);
    $course_n = mysqli_affected_rows($mysqli);
    if($course_n == 1)
    {
        // Get the insert id before closing the connection
        $insert_id = mysqli_insert_id($mysqli);


        // Note that there probably isn't a good reason to explicitly close the
        // connection here.  It will be closed when the script terminates.
        mysqli_close($mysqli);

        // And return it.
        return $insert_id;
    }
    mysqli_close($mysqli);
    return false;
}

require "/mysqli_connect.php";
$course = new Course();
$c = $course->createCourse();
echo $c;
// $c contains the id

设计类使用一个公共连接:

该类在其构造函数中接收连接。

// Make classes accept the connection as a param:
class MyClass
{
  // property to hold the connection object
 public $db;

  // constructor receives the connection
  public function __construct($connection) {
    $this->db = $connection;
  }
  
  // Methods call it as $this->db
  public function createCourse() {
    $course_r = mysqli_query($this->db, $course_q);
  }
      
}

仅在控制脚本

上包含一次连接
require_once("/mysqli_connect.php");
// $mysqli is now defined...

// Pass it into the constructor
$course = new MyClass($mysqli);

答案 1 :(得分:2)

问题是您有多个与数据库的连接,并且基于每个连接跟踪最后插入的ID。

注意:每次运行查询时,都应该连接到同一个数据库。只需在脚本的开头连接,然后在结束时关闭。不断连接和断开连接会导致脚本运行速度极慢,尤其是查询次数增加时。

此外,最后插入的ID可通过MySQLi的mysqli_insert_id()函数获得。

答案 2 :(得分:0)

最有可能的原因是你在createCourse()结束时关闭了mysqli连接。

不要让createCourse()负责关闭连接(将其移至其他位置)或让createCourse()成功返回插入ID(或失败时false