Undefined property: stdClass::$news_title

I use the following function to check if there are any new news items:


	function get_news(){
        $cn = "SELECT MIN(news_id) AS news_id
                    , news_title
				 FROM news
			    WHERE isNew = ?";
				
         $chNew = $this->pdo->prepare($cn);
         $chNew->execute(array($isNew));
		 $res_new = $chNew->fetch();
		 $return = '';		   		
		if (empty($res_new)){			
			echo "There are no new news items";
		}else{
            $chNew = $this->pdo->prepare($cn);
            $chNew->execute(array($isNew));
			while($row = $chNew->fetch(PDO::FETCH_OBJ)) {
				$return .= '<p>'.htmlspecialchars($row->news_title).'</p>';
				
			}
			return $return;
		}

When executing the page while there are new news items I get the following error:

Undefined property: stdClass::$news_title

What should I do to avoid that?

Preparing and executing that query twice makes no sense and $isNew is not defined.

$row->news_title is where the warning is coming from. It means that the Object $row doesn’t have a property called ‘news_title’. If you want to know what that object looks like in the inside, use var_dump($row) and you’ll see what kind of property it has.

And, like oddz said, the variable $isNew isn’t define anywhere. Shouldn’t you put ‘1’ instead of this variable?

@oddz The variable isNew is defined at the very top of the class:


$isNew       = 1;

@xMog You were right about the news_title. It was a typo. It is working fine now.

Thank you both a lot.