mysqli自动增加生成的主要ID

Ari*_*osh 3 php mysql mysqli

$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);
Run Code Online (Sandbox Code Playgroud)

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

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

echo $statement->insert_id; 
Run Code Online (Sandbox Code Playgroud)

小智 6

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

$statement = $mysqli->query($insert);
echo $mysqli->insert_id; // correct
echo $statement->insert_id; //not correct
Run Code Online (Sandbox Code Playgroud)

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