【问题标题】:PHP MYSQL PDO lastInsertId is returning 0 when using a procedure to insert rows [duplicate]使用过程插入行时,PHP MYSQL PDO lastInsertId 返回 0 [重复]
【发布时间】:2013-03-11 20:17:26
【问题描述】:

我的 LastInsertedID 一直为零,我认为这是因为我使用的是 mysql 存储过程。这是我的代码:

// Set up a new MYSQL PDO Database Connection
$dbConnection = new PDO('mysql:host=localhost;dbname=database;charset=UTF8', 'username', 'password');
//turn off emulation for prepared statement and set error mode option
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Create a statement and prepare it with parameters
$stmt = $dbConnection->prepare("CALL AddMovieSet(:UserID, :SelectedLibraryID, :txtName, :txtNotes);");
$stmt->bindValue(':UserID', $_SESSION["UserID"], PDO::PARAM_STR);
$stmt->bindValue(':SelectedLibraryID', $_SESSION["SelectedLibraryID"], PDO::PARAM_STR);
$stmt->bindValue(':txtName', $_GET["txtName"], PDO::PARAM_STR);
$stmt->bindValue(':txtNotes', $_GET["txtNotes"], PDO::PARAM_STR);
//execute the prepared statement
$stmt->execute();

//the stored procedure successfully inserts a row
$Name=$_GET["txtName"];
$Notes=$_GET["txtNotes"];
$InsertedID=$dbConnection->lastInsertId(); 
//$InsertedID is always zero, the table I am inserting a row into has an AUTO_INCREMENT for the first column.

【问题讨论】:

    标签: php mysql pdo last-insert-id


    【解决方案1】:

    有时 PHP 和 PDO 可能会出现 lastInsertID() 和存储过程的错误。尝试改用这个 MySQL 调用:

    "SELECT LAST_INSERT_ID();"
    

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      相关资源
      最近更新 更多