简体   繁体   English

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

[英]Trying to get property of non-object with 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; 代码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. 根据文档, findByPk返回找到的记录;如果未找到,则返回Null 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;     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM