douqiang3768 2015-12-31 13:47
浏览 56
已采纳

PDO - 注意:尝试获取非对象的属性

What is the problem in this function?

function journeygetLeadertalks($link, $journeyid)
{
    $thingResult = $link->prepare("SELECT `leadertalks` FROM `journey` WHERE journeyid=:journeyid");
    $thingResult->execute([
        "journeyid" => $journeyid
    ]);


    if($thingResult->fetchObject()->leadertalks == "NULL")
        return "imgs/noimg.png";
    else
        return $thingResult->fetchObject()->leadertalks;
}

Database picture: This function works every time exepct when im using it for leadertalks and leadertalksImage, what can I do?

  • 写回答

1条回答 默认 最新

  • douyiyi5284 2015-12-31 13:54
    关注

    The problem is that you're calling fetchObject() twice. You're calling it once in the if, and then if the value doesn't match, you're calling it again in the else. But your query only returns one row, so the second fetchObject() returns false, and that's not an object. You need to save the result in a variable.

    Also, if the query doesn't match anything at all, the initial fetchObject() will return false, so you should check for that as well.

    $row = $thingResult->fetchObject();
    if ($row && $row->leadertalks == "NULL") {
        return "imgs/noimg.png";
    } else {
        return $row->leadertalks;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部