Trying To Get Property Of Non-Object

When I am executing following code, it gives error message "Trying to get property of non-object " relevant to the line "$receiptNo = $row->receiptNo;". Can someone help me to fix this, please. Thanks.

public function removeZeroBalanceRecords(){

179 //

180 $connection=Yii::app()->db;

181 $dataReader=$connection->createCommand("SELECT DISTINCT receiptNo FROM tblPaid");

182 foreach ($dataReader as $row) {

183 $receiptNo = $row->receiptNo;

184 $sqlInsert=‘INSERT INTO tblPaidSettled (SELECT * FROM tblPaid WHERE receiptNo="$receiptNo")’;

185 $command=$connection->createCommand($sqlInsert);

186 //

187 //

188 }

189 }

try this … its working here with me




$dataReader = Yii::app()->db->createCommand('SELECT DISTINCT receiptNo FROM tblPaid')->queryAll();


foreach ($dataReader as $row) {

  $receiptNo = $row['receiptNo'];

}

:(

It is working, Thanks

:P

RohanP: Just a small info. Always use code snipset (second button to the right on second toolbar – <>) to format your code and make it more readable by others. Thanks!

[/size]

You have created an SQL command, but have you actually executed it?

Look into the documentation, for createCommand function. It says: "Creates a command for execution". It says nothing about actually executing created command.

Since you don’t execute your command, database is not polled, you’re not getting any results and thus your dataReader contains nothing (or some garbage) – no objects you could use.