[英]How can I get a column from database based on the value of the column
我想知道如何通过使用列的值从数据库中检索一项,我有一个名为sid的列的数据库项目,该数据库项目根据其用途具有不同的值,我有一个与sid为“描述”,另一个为sid值为“ profile_view”。 我如何通过使用php PDO获得具有description值的代码,这是我尝试使用的代码:
try {
$GetSettings = $db->prepare("SELECT * FROM `profile_settings` WHERE profileId=?");
$GetSettings->bindValue(1, $profileId, PDO::PARAM_STR);
$GetSettings->execute();
$SettingsData = $GetSettings->fetchAll(PDO::FETCH_ASSOC);
foreach ($SettingsData as $SettingsRow) {
$description = $SettingsRow['sid']['description'];
$profile_view = $SettingsRow['sid']['profile_view'];
}
} catch (PDOException $e) {
}
$SettingsRow['sid']
不是关联数组,它是具有该行中sid
列值的字符串。 因此,您需要将其与所需的值进行比较,然后从另一列获取该值。
foreach ($SettingsData as $settingsRow) {
$sid = $SettingsRow['sid'];
$value = $SettingsRow['value']; // I'm just assuming this is the name of the column with the relevant data
if ($sid == 'description') {
$description = $value;
} elseif ($sid == 'profile_view') {
$profile_view = $value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.