尝试使用PHP获取非对象的属性

I have a strange issue; my code is working on my localhost, but when I try to use it online. It's showing an error in the return line:

Trying to get property of non-object

Here is the code:

public static function displayContenuAsString ($id,$class="traduction survol_video",$div="div") {
        return "<$div id=\"contenu_$id\" class=\"$class\"></$div>".Contenu::model()->findByPk($id)->valeur;     
    }

No issues in the code Contenu::model()->findByPk($id)->valeur; Just check for the row in database with primary key.

According to documentation findByPk return the record found or Null if none is found. So you need to add check before using model values in this way:

public static function displayContenuAsString ($id,$class="traduction survol_video",$div="div") {
    $contenu = Contenu::model()->findByPk($id);
    $valeur = $contenu !== null ? $contenu->valeur : 'Empty';
    return "<$div id=\"contenu_$id\" class=\"$class\"></$div>".$valeur;     
}