gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:56:32 24 4
gpt4 key购买 nike

我想知道如何使用列的值从数据库中检索项目,我有一个数据库项目,其中有一个名为 sid 的列,该项目根据其用途而具有不同的值,我有一个sid 为“description”的项目和另一个 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) {

}

最佳答案

$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;
}
}

关于php - 如何根据列的值从数据库中获取列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37579155/

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