我有以下代碼。應該返回表的最後一行的mysqli_insert_id()(在本例中爲「$ last_row」)總是返回0.爲什麼這樣呢?爲什麼mysqli_insert_id()總是返回0?
<?php
include('connect-db.php');
$submit_date = date("Ymd");
$content = mysqli_real_escape_string($connection, htmlspecialchars($_POST['editor']));
$ip_address = $_SERVER['REMOTE_ADDR'];
$last_row = mysqli_insert_id($connection);
if ($content != '') {
$sql = "INSERT INTO source (track_id, submit_date, ip, content) VALUES ('$last_row', '$submit_date','$ip_address','$content')";
if (!mysqli_query($connection,$sql))
{
die('Error: ' . mysqli_error($connection));
}
echo $last_row;
mysqli_close($connection);
}
?>
「*返回值:AUTO_INCREMENT字段的值這由前面的查詢更新**如果在連接上沒有以前的查詢,或者查詢沒有更新AUTO_INCREMENT值,則返回零**。*「 – DCoder 2013-03-11 06:14:13
Look:http://php.net/manual/en /mysqli.insert-id.php – 2015-02-12 01:08:49