如何根据列的值从数据库中获取列

时间:2016-06-01 21:33:26

标签: php mysql pdo

我想知道如何通过使用列的值从数据库中检索项目,我有一个数据库项目,其中包含一个名为sid的列,根据应该使用的内容具有不同的值,我有一个带有"描述"的sid的项目另一个sid值为" profile_view"。 如何通过使用php PDO获取具有描述值的那个,这是我尝试使用的代码:

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) {

}

1 个答案:

答案 0 :(得分:0)

$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;
    }
}
相关问题