简体   繁体   中英

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_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

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

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

它是mysql_insert_id()而不是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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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