我尝试使用以下代码获取数据
$result = $viral->getData("SELECT * FROM preview WHERE id=$id");
foreach ($result as $res) {
$title = $res['title'];
$promoted = $res['promoted'];
$photo = $res['file'];
}
我用这段代码查看查询结果
<p><?php echo $title;?></p>
我想当数据库中的值$ title为空时,它将自动替换为我的默认值。
当变量$ title为空时,我可以给出默认值吗?
答案 0 :(得分:2)
试试这个
$title = $res['title'];
echo "<p>" . (($title == "" || $title == NULL) ? "Default Value" : $title) . "</p>"
答案 1 :(得分:0)
这可以这样做,请尝试
$result = $viral->getData("SELECT * FROM preview WHERE id=$id");
foreach ($result as $res) {
$title = empty($res['title'])? "Your Default Valur or Variable":$res['title'];
$promoted = $res['promoted'];
$photo = $res['file'];
}
或者如果您只使用$title
变量,请使用此
empty($title)? "Your Default Valur or Variable":$title;
答案 2 :(得分:0)
你可以尝试
$title = $res['title'];
if (empty($title){
$title = "assign default title";
}