A
A
Alexander2015-10-10 13:36:02
Yii
Alexander, 2015-10-10 13:36:02

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

1 answer(s)
M
Maxim Timofeev, 2015-10-10
@webinar

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 question

Ask a Question

731 491 924 answers to any question