Get Number Of Rows

How can I access the returned row with the counted value in the query below?

That seems like a strange way to do it. Try something like this instead:

$sql = "SELECT COUNT(*) FROM tbl_test";

$command = Yii::app()->db->createCommand($sql);

$count = $command->queryScalar();

1 Like

That seems to be one step closer, but the $count is currently always 1 as it will return the number of returned rows. However, I need to know the value in the returned count.

I don’t understand. Are you trying to return the last row in the database table?

No, I’m trying to get the actual value of count(*).

Let’s say that tbl_test contains the following:

Row 1 = "A"

Row 2 = "B"

Then count(*) should return 2 and the value I’m looking for is 2.

queryScalar() returns the the value of the first column of the first row, not the number of rows -

This should work though as I am only retrieving the first column of the first row, so Keith is right. But not sure why it’s not working.

Could you post the code that you’re using?

Thanks guys, I got it working. The code Keith posted was correct, I just had an issue with my logic.

Dear Friend

Kindly check whether the following serve the purpose.

Attach the property in model TestManager.

public $c;

Then make the following query.

$criteria=new CDbCriteria;

$criteria->select="COUNT(*) AS c FROM tbl_test";

$result = TestManager::model()->find($criteria);

if ($result->c > 0) {

$accessGranted = true;