Answer the question
In order to leave comments, you need to log in
How to get the last id in a table correctly?
We need to get the most recent id from the table.
So far I decided to do this, but it does not plow:
public function actionIndex()
{
$db = Yii::$app->db;
$sql = "SELECT id FROM articles";
Yii::$app->db->createCommand($sql)->execute();
$db = Yii::$app->db->getLastInsertID();
echo $db;
die();
}
Answer the question
In order to leave comments, you need to log in
Your id is an autoincrement, the largest is the latest, you can select all records, and then use max() or sort and select the 1st element. The question is why do you need this, since there may be nuances.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question