繁体   English   中英

mysql_insert_id不返回任何内容

[英]mysql_insert_id doesn't return anything

$sql="INSERT INTO wp_comments (comment_post_ID, comment_author, comment_date, comment_content, user_id) 
                VALUES
              ('$qID', '$author', '$dro', '$content', '$_SESSION[user_id]')";

            $result = mysql_query($sql);
            die(last_insert_id());

当我运行此代码时,我只会看到白色屏幕,因此last_insert_id()似乎没有返回任何值...我在做什么错?

mysql_insert_id()可能是您要检索的最后插入行的ID的函数。

http://php.net/manual/zh/function.mysql-insert-id.php

从PHP手册中退出(die是exit的别名):

如果status是字符串,此函数将在退出前打印状态。

如果status是一个整数,则该值将用作退出状态并且不打印。 退出状态应在0到254的范围内,退出状态255由PHP保留,不得使用。 状态0用于成功终止程序。

如果last_insert_id()返回一个整数,则不会打印该整数。

顺便说一句,last_insert_id不是内置的PHP函数。 您还应该确保使用正确的功能。

请尝试以下操作(假设定义了last_insert_id函数):

print last_insert_id();
exit();

如前所述,首先检查mysql_error()mysql_errno() 捕获它们的一个好方法是:

if (mysql_errno()==0) {
    // all was good
    $last_insert_id = mysql_insert_id();
}
else {
    // error occurred
    echo mysql_error();
}

它是mysql_insert_id()而不是last_insert_id();

您需要在PHP内将last_insert_id作为常规MYSQL查询运行。 像这样:

 $result = mysql_query("SELECT LAST_INSERT_ID() FROM wp_comments");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM