gpt4 book ai didi

php - PDO/MySQL 使用 if 语句获取多列

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:27 25 4
gpt4 key购买 nike

我目前正在尝试从我的数据库中获取两个图像位置,我如何返回两个列,如果它们都是空的,则回显另一个图像。这是我到目前为止所得到的。如何返回 photo 和 photo_small 以便我可以在 php 文件中回应它们。

PUBLIC FUNCTION Profile_Pic($uiD) {
$sth = $this->db->prepare("SELECT photo,photo_small FROM users WHERE uiD = :id");
$sth->execute(array(':id' => $uiD));

if ($sth->rowCount() > 0) {
$data = $row['photo'];
return $data;
} else {
$data = './icons/users.png';
return $data;
}
}

最佳答案

PUBLIC FUNCTION Profile_Pic($uiD) {
$sql = "SELECT photo,photo_small FROM users WHERE uiD = ?";
$sth = $this->db->prepare($sql);
$sth->execute(array($uiD));
$data = $sth->fetch();
if (empty($data['photo'])) {
$data['photo'] = './icons/users.png';
}
if (empty($data['photo_small'])) {
$data['photo_small'] = './icons/users.png';
}
return $data;
}

如果你想替换两个图像,即使一个图像都不存在

PUBLIC FUNCTION Profile_Pic($uiD) {
$sql = "SELECT photo,photo_small FROM users WHERE uiD = ?";
$sth = $this->db->prepare($sql);
$sth->execute(array($uiD));
$data = $sth->fetch();
if (empty($data['photo']) || empty($data['photo_small'])) {
$data['photo'] = './icons/users.png';
$data['photo_small'] = './icons/users.png';
}
return $data;
}

关于php - PDO/MySQL 使用 if 语句获取多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16976853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com