Get Last Record In Db

how can i get the last id inserted in a database?.. i have a table called chat and i need to get the last from it

this should do it


$row = Yii::app()->db->createCommand()

                ->select('id')

                ->from('chat')

                ->order('id DESC')

                ->queryRow();

        echo $row['id'];

$criteria = new CDbCriteria;

	$criteria->order     = "EmpID desc";


	$criteria->select    = "EmpID";


	$empModel			 = Employee::model()->find($criteria);


	echo $empModel->EmpID;	

OR to get after immediately saving,

	$objEmp				 	= new Employee();


	$objEmp->EmpName 	 	= "testuser";


	$objEmp->EmpDesignation = "tester";


	$objEmp->DepartID 		= "1";


	


	$objEmp->save();


	echo $objEmp->EmpID;

As notted by RKA, if you are using the AR model, the PK field gets automatically updated after the save() so you can get it right from there.

Or you can use the getLastInsertID() method - http://www.yiiframework.com/doc/api/1.1/CDbConnection#getLastInsertID-detail

@Domba I believe He just wants to get the last record from the database not the last insert ID maybe I misunderstood

lol… the question say "how can I get the last id inserted"… also your first comment ends with


echo $row['id'];

in any case northertus is here to make it clear ;)

@domba

this is what the title says

here is the second sentence

what do you make of it

as far as my last comment

this was just to demonstrate how access the data since queryRow returns an array

sorry for taking so long to answer, i live in venezuela and here is getting hard to work due the crisis.

anyways, to be more clear about what im asking for… i have a table called chat, i need to get only the last id found in this table without doing a save on record