How to get ID of the last updated row in MySQL?

后端 未结 12 2270
北海茫月
北海茫月 2020-11-22 08:10

How do I get the ID of the last updated row in MySQL using PHP?

12条回答
  •  鱼传尺愫
    2020-11-22 08:42

    Query :

    $sqlQuery = "UPDATE 
                update_table 
            SET 
                set_name = 'value' 
            WHERE 
                where_name = 'name'
            LIMIT 1;";
    

    PHP function:

    function updateAndGetId($sqlQuery)
    {
        mysql_query(str_replace("SET", "SET id = LAST_INSERT_ID(id),", $sqlQuery));
        return mysql_insert_id();
    }
    

    It's work for me ;)

提交回复
热议问题