I have two questions. Please help at least one. The first question: how to find out the ID of the last inserted record?

There is a code:

// Добавляем новую запись в таблицу mysql_query('INSERT INTO users SET name="Nick"'); // Определяем идентификатор последней добавленной записи в этой таблице $latest_id = mysql_insert_id(); print 'Самая последняя запись имеет ID: '.$latest_id; 

BUT the problem is that after the request, before the mysql_insert_id () command, any number of inserts can be executed, and the code is not relevant here, this function shows the last ID of the inserted record not of the table, but of the database, i.e. which last record was inserted into one of the database tables is displayed.
Question number two: and most importantly, you need to implement for OOP, namely for the yii framework. While it looks like this:

 $sql="INSERT INTO `users` (`name`) VALUES ('Nick')"; Yii::app()->db->createCommand($sql)->execute(); 

    2 answers 2

    The id of the inserted table in yii is as follows:

     Yii::app()->db->getLastInsertID() 

      Create a User model, and then

       $user = new User(); $user->name = "Nick"; if(!$user->save()) throw new Exception("Failed create new user!");