mysqli auto incremented primary id that was genera

2020-04-11 07:48发布

问题:

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

How do I get this AUTO INCREMENT that was generated by MySQL? I've tried this but it doesn't work:

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

echo $statement->insert_id; 

回答1:

Insert id is a property of the MYSQLI object and not the MYSQLI result object:

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

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



回答2:

You can get the auto_increment value with

$id = mysqli_insert_id($mysqli);

See mysqli_insert_id for more info.



标签: php mysql mysqli