简体   繁体   English

MySQL PHP PDO获取列中值为“是”的所有用户

[英]MySQL PHP PDO Fetch all users with a value of Yes in column

i have the following PDO to get the users data using the session if it is set. 如果设置了会话,我可以使用以下PDO来获取用户数据。

$userClass = new USER();

if (!isset($_SESSION['SESSION'])) {
    $userID = isset($_SESSION['SESSION']); //removes session not set error
} else {
    $userID = $_SESSION['SESSION'];
}

$stmt = $userClass->runQuery("SELECT * FROM users WHERE userID=:userID");
$stmt->execute(array(":userID"=>$userID));
$user = $stmt->fetch(PDO::FETCH_ASSOC);

using the following if the user is logged in i can display any column that the user has. 如果用户已登录,则使用以下命令,我可以显示用户拥有的任何列。

using $user['Username'] or check if they are a model or not $user['Model'] would result in a Yes or No response. 使用$user['Username']或检查它们是否为模型$user['Model']将导致是或否响应。

i have a public page where i would like to display all the users with the Model response of Yes, even if the session is not set, as this is a public display of the model page. 我有一个公共页面,即使没有设置会话,我也想显示所有具有模型响应的用户,因为这是模型页面的公共显示。 I'm not sure how to grab that information without the need of a session. 我不确定如何无需会话即可获取该信息。 i will include a "?" 我将包含一个“?” into the code. 进入代码。 that represent the part i am not sure what to put in. 代表那部分我不确定该放入什么。

$userClass = new USER();
$Models = 'Yes'; // ?
$stmt = $userClass->runQuery("SELECT * FROM users WHERE Model=:Model");
$stmt->execute(array(":Model"=>$Models));
$isModel = $stmt->fetch(PDO::FETCH_ASSOC);

I'm not sure if the $Models variable should be preset to Yes because if the response is No then it should not display the user else if it is yes display all users with Yes in the Model column ? 我不确定$Models变量是否应预设为“是”,因为如果响应为“否”,则它不应显示用户,否则为“是”,则在“模型”列中显示所有具有“是”的用户?

any help would be appreciated 任何帮助,将不胜感激

Here is how i solved getting user data without having to have a session set. 这是我解决的方法,无需设置会话即可获取用户数据。 Using PDO. 使用PDO。

    $getData = $userClass->runQuery('SELECT Username, Model FROM users');
    $getData->execute();
    $result = $getData->fetchAll(PDO::FETCH_KEY_PAIR);
    $jsonEN = json_encode($result);
    $jsonDE = json_decode($jsonEN, true);

foreach ($jsonDE as $k => $v) {
    if ($v == "No") {
        // Do nothing if No
    } elseif ($v == "Yes") {
        print $k . " " . $v;
    }
}

the following prints out the Username plus the value Yes and if it's No it does nothing. 以下将打印出用户名和值“是”,如果不存在,则不执行任何操作。

Username Yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM