Reputation: 127
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?
Upvotes: 0
Views: 656
Reputation: 782166
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;
}
Upvotes: 1