Peter Torsten Nilsson Peter Torsten Nilsson - 4 months ago 4x
SQL Question

How can I get a column from database based on the value of the column

I was wondering how I can retrive an item from a database by using the value of the column, I have a database item with a column called sid, that have different values based on what it should be used for, i have one item with the sid of "description" and another with the sid value of "profile_view".
How can I get the one with the value of description by using php PDO, this is the code I am trying to use:

try {
$GetSettings = $db->prepare("SELECT * FROM `profile_settings` WHERE profileId=?");
$GetSettings->bindValue(1, $profileId, PDO::PARAM_STR);
$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'] is not an associative array, it's a string with the value of the sid column in that row. So you need to compare it with the values you're looking for, then get the value from another column.

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;