Reputation: 11308
$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?
Upvotes: 0
Views: 286
Reputation: 2377
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
Upvotes: 4
Reputation: 53960
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();
Upvotes: 1
Reputation: 39389
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();
}
Upvotes: 1
Reputation: 181290
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");
Upvotes: 0