3
$insert = "INSERT INTO event_tracker_table (event_name, event_location, location_number, event_creator_username, event_creator_email)

            VALUES (
                    '".$_POST['event_name']."',
                    '".$_POST['event_location']."',
                    '".$_POST['location_number']."',
                    '".phpCAS::getAttribute('uid')."',
                    '".phpCAS::getAttribute('mail')."'
                    )";


            $mysqli->query($insert);

如何获得由 MySQL 生成的这个 AUTO INCREMENT?我已经尝试过了,但它不起作用:

$statement = $mysqli->query($insert);

echo $statement->insert_id; 
4

2 回答 2

6

插入 id 是 MYSQLI 对象的属性,而不是 MYSQLI 结果对象:

$statement = $mysqli->query($insert);
echo $mysqli->insert_id; // correct
echo $statement->insert_id; //not correct

http://php.net/manual/en/mysqli.insert-id.php

于 2012-11-23T21:14:38.400 回答
0

您可以通过以下方式获取 auto_increment 值

$id = mysqli_insert_id($mysqli);

有关更多信息,请参见mysqli_insert_id

于 2012-11-23T21:14:10.177 回答