开发者

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());

When I run this code I see white screen only, so last_开发者_C百科insert_id() doesn't seem to return any value... What am I doing wrong?


mysql_insert_id() may be the function you are looking for to retrieve the id of the last inserted row.

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


As aforementioned, check mysql_error() or mysql_errno() first. A good way to catch them would be:

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


From the PHP manual on exit (die is an alias of exit):

If status is a string, this function prints the status just before exiting.

If status is an integer , that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

If last_insert_id() returns an integer, it won't be printed.

By the way, last_insert_id is not a built-in PHP function. You should also make sure you are using the correct function.

Try this instead (supposing the last_insert_id function is defined):

print last_insert_id();
exit();


It is mysql_insert_id() not last_insert_id();


You need to run last_insert_id as a regular MYSQL query inside PHP. Something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜