Accessing last created row in PHP/MySQL

前端 未结 4 1885
面向向阳花
面向向阳花 2020-12-11 23:06

How to access the row which has just been inserted into a DB with PHP/MySQL?

I have:

    $sql = \'INSERT INTO `jos_db`.`jos_sections` (`id`,  `name`)         


        
4条回答
  •  青春惊慌失措
    2020-12-11 23:50

    If you 'id' column is an auto-increment, you can use mysql_insert_id :

    Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.


    The example given in the manual looks like this :

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('mydb');
    
    mysql_query("INSERT INTO mytable (product) values ('kossu')");
    printf("Last inserted record has id %d\n", mysql_insert_id());
    

提交回复
热议问题